Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🔥 Add support or documentation for unit tests #41

Closed
schweigert opened this issue Feb 5, 2020 · 10 comments
Closed

🔥 Add support or documentation for unit tests #41

schweigert opened this issue Feb 5, 2020 · 10 comments

Comments

@schweigert
Copy link

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.

@Fenny
Copy link
Member

Fenny commented Feb 5, 2020

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

@Fenny
Copy link
Member

Fenny commented Feb 5, 2020

What do you think @schweigert? Let us know so we can implement this feature 👍

@schweigert
Copy link
Author

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.

@Fenny
Copy link
Member

Fenny commented Feb 6, 2020

Maybe we should drop the raw http string and only work with http.Request & http.Response.
@koddr, what do you think?

@koddr
Copy link

koddr commented Feb 6, 2020

@Fenny I think, we can do this, if it's solve this issue 👍
@schweigert can you send PR for this fix?

@Fenny
Copy link
Member

Fenny commented Feb 6, 2020

@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!

@PK2702
Copy link

PK2702 commented Feb 7, 2020

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

```@

> ****

@pjebs
Copy link
Contributor

pjebs commented Jan 28, 2021

How can I use this feature from a _test.go file? It appears you need a reference to app variable.

@yaliv
Copy link

yaliv commented Jan 28, 2021

How can I use this feature from a _test.go file? It appears you need a reference to app 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)
}

@cdunn2001
Copy link

I wish the wiki were in GitHub. https://fiber.wiki is currently unavailable.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants