Comments (10)
How can I use this feature from a
_test.go
file? It appears you need a reference toapp
variable.
You can prepare a function, which creates a Fiber app and use middleware(s) that you want to test.
For example:
func setupApp() *fiber.App {
app := fiber.New()
app.Use(authenticate.New(), attachuser.Handle, authorize.Handle)
authenticateduser.Setup(app)
return app
}
Then use that function in your test codes:
const bearerToken = "Bearer o8SMWsPx.9wJ7nZ86.XUyQV3yu"
func TestGet(t *testing.T) {
app := setupApp()
req := httptest.NewRequest("GET", "/", nil)
req.Header.Set("Authorization", bearerToken)
res, _ := app.Test(req)
}
func TestPatch_Profile(t *testing.T) {
app := setupApp()
const reqBody = `{
"scope": "profile",
"data": {
"name": "My New Name"
}
}`
req := httptest.NewRequest("PATCH", "/", strings.NewReader(reqBody))
req.Header.Set("Authorization", bearerToken)
res, _ := app.Test(req)
}
func TestPatch_Password(t *testing.T) {
app := setupApp()
const reqBody = `{
"scope": "password",
"data": {
"oldPassword": "qwerty",
"newPassword": "12qwaszx"
}
}`
req := httptest.NewRequest("PATCH", "/", strings.NewReader(reqBody))
req.Header.Set("Authorization", bearerToken)
res, _ := app.Test(req)
}
from fiber.
Thanks for your kind words @schweigert, welcome!
What about a method named
Test
so you can pass a raw HTTP string or*http.Request
struct to test your application locally.Raw HTTP string
package main import ( "fmt" "github.com/gofiber/fiber" ) func main() { app := New() app.Get("/demo", func(c *Ctx) { fmt.Println(c.BaseURL()) // => http://google.com }) body, err := app.Test("GET /demo HTTP/1.1\r\nHost: google.com\r\n\r\n") fmt.Println(body, err) }Using http.Request
package main import ( "fmt" "github.com/gofiber/fiber" ) func main() { app := New() app.Get("/demo", func(c *Ctx) { fmt.Println(c.BaseURL()) // => http://google.com }) req, _ := http.NewRequest("GET", "http://google.com/demo", nil) req.Header.Set("X-Custom-Header", "hi") body, err := app.Test(req) fmt.Println(body, err) }
```@
> ****
from fiber.
@Fenny I think, we can do this, if it's solve this issue 👍
@schweigert can you send PR for this fix?
from fiber.
@koddr I will make a PR, thnx for the feedback.
@schweigert => https://fiber.wiki/#/application?id=test
I'm closing this topic now, thank you!
from fiber.
Thanks for your kind words @schweigert, welcome!
What about a method named Test
so you can pass a raw HTTP string or *http.Request
struct to test your application locally.
Raw HTTP string
package main
import (
"fmt"
"github.com/gofiber/fiber"
)
func main() {
app := New()
app.Get("/demo", func(c *Ctx) {
fmt.Println(c.BaseURL()) // => http://google.com
})
body, err := app.Test("GET /demo HTTP/1.1\r\nHost: google.com\r\n\r\n")
fmt.Println(body, err)
}
Using http.Request
package main
import (
"fmt"
"github.com/gofiber/fiber"
)
func main() {
app := New()
app.Get("/demo", func(c *Ctx) {
fmt.Println(c.BaseURL()) // => http://google.com
})
req, _ := http.NewRequest("GET", "http://google.com/demo", nil)
req.Header.Set("X-Custom-Header", "hi")
body, err := app.Test(req)
fmt.Println(body, err)
}
from fiber.
What do you think @schweigert? Let us know so we can implement this feature 👍
from fiber.
This looks good, but I still can't test the status code of the request. I believe that the return should not be a string, but a response, from the http package.
from fiber.
Maybe we should drop the raw http string
and only work with http.Request
& http.Response
.
@koddr, what do you think?
from fiber.
How can I use this feature from a _test.go
file? It appears you need a reference to app
variable.
from fiber.
I wish the wiki were in GitHub. https://fiber.wiki is currently unavailable.
from fiber.
Related Issues (20)
- 🐛 [Bug]: Logger middleware default Format isn't the format that's used HOT 3
- 🐛 [Bug]: Missing ContextKey in requestid middleware configuration HOT 2
- 🤗 [Question]: Problem with CSRF after updating fiber to 2.51 HOT 10
- 🐛 [Bug]: utils.IsIPv4 and net.ParseIP have inconsistent results HOT 5
- 🚀 [Feature]: middleware/logger: use Logger interface HOT 4
- 🤗 [Question]: how to debug requests in Fiber within GoLand HOT 2
- 🐛 [Bug]: Inconsistency in Session and CSRF Middleware Handling of Timeout and Expiration HOT 11
- 🐛 [Bug]: Csrf with Session store does not support secure cookie middleware HOT 4
- 📝 [v3 Proposal]: Parity for res.format and Ctx.Format HOT 4
- 🤗 [Question]: PR template and CONTRIBUTING.md Inconsistency HOT 1
- The compiled package is too big, isn't it HOT 6
- 🐛 [Bug]: Middleware Rate Limiter Not Work as expected if Header Value Using IP HOT 2
- 🐛 [Bug]: Very rare panics in ctx.BodyParser or in fasthttp under unknown conditions HOT 14
- 🚀 [Feature]: Implement Generic APIs for Enhanced Variable Type HOT 8
- 🤗 [Question]: I got incorrect data when using map for recording in the middleware
- 🤗 [Question]: Can fiber support reverse url like django? HOT 3
- 🚀 [Feature]: Allow rebuilding route tree stack HOT 2
- 🐛 [Bug]: CORS middleware should ignore config AllowedOrigins when AllowedOriginsFunc is defined HOT 1
- Why isn't c.Params(param) capturing the route parameter in a middle handler? HOT 6
- 🤗 [Question]: best way to check if a path is registered in the app HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from fiber.