I loved the framework. Simple and straightforward. Excellent for making microservices. However, I didn't find any support to create unit tests :(
The use of the gin framework is precisely because it allows the use of httptest as a standard, which is already well documented.
Maybe it would be nice to make a mock of the context to test the behaviors, and be able to access all the registered routes if you want to test the association between middleware, methods and routes.
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)
}
What do you think @schweigert? Let us know so we can implement this feature :+1:
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.
Maybe we should drop the raw http string and only work with http.Request & http.Response.
@koddr, what do you think?
@Fenny I think, we can do this, if it's solve this issue 馃憤
@schweigert can you send PR for this fix?
@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!
Thanks for your kind words @schweigert, welcome!
What about a method named
Testso you can pass a raw HTTP string or*http.Requeststruct 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) }
```
```@