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)
- Improve Error Handling in CSRF Middleware Storage
- ๐ค [Question]: v3 when ready for production HOT 1
- ๐ [Bug]: Unclear "json: invalid use of ,string struct tag, trying to unmarshal unquoted value into uint64" HOT 9
- ๐ค [Question]: Cache Next is not called when KeyGenerator is Defined HOT 6
- ๐ [Bug]: v3 Flash Message with redirect is not working HOT 3
- ๐ [Proposal]: Upgrade fiber.NewError() for error handle HOT 4
- ๐ [Proposal]: Reduce memory usage for "prefork" mode of the master process HOT 2
- ๐งน [Maintenance]: Add Parallel Benchmarks
- ๐ค [Question]: zero allocation - why is this any different from how Chi or others behave? Can you document an example of what you mean? HOT 8
- ๐ค [Question]: FX with Fiber graceful shutdown Failed to stop cleanly: context deadline exceeded HOT 15
- ๐ค [Question]: whats the session id injection issue in github report HOT 4
- ๐ [Bug]: Mutex issues with Idempotency middleware
- ๐ [Bug]: Logger module causes OOM with ${bytesSent} and ${bytesReceived} HOT 10
- ๐ค [Question]: limiter supports dynamic setting of โMaxโ parameter HOT 8
- ๐ [Proposal]: Add StartupProbe support to healthcheck Middleware HOT 6
- [Question]: Can't debug in Goland in Routes HOT 2
- ๐ [Bug]: cache middleware: runtime error: index out of range [0] with length 0 HOT 15
- ๐ [Proposal]: Get session by its ID HOT 1
- ๐ [Proposal]: Support for Application State Management HOT 1
- ๐ [Bug]: Middleware Executes Twice When app.Static is Placed Before Any Middleware 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.