Fiber: 馃敟 Add support or documentation for unit tests

Created on 5 Feb 2020  路  7Comments  路  Source: gofiber/fiber

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.

All 7 comments

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 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)
}

```

```@


Was this page helpful?
0 / 5 - 0 ratings

Related issues

jeyraj picture jeyraj  路  4Comments

peterbourgon picture peterbourgon  路  4Comments

mhf-ir picture mhf-ir  路  3Comments

renatojf picture renatojf  路  3Comments

faultable picture faultable  路  3Comments