Code Monkey home page Code Monkey logo

Comments (10)

yaliv avatar yaliv commented on May 1, 2024 4

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

from fiber.

PK2702 avatar PK2702 commented on May 1, 2024 2

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.

koddr avatar koddr commented on May 1, 2024 1

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

from fiber.

Fenny avatar Fenny commented on May 1, 2024 1

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

Fenny avatar Fenny commented on May 1, 2024

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 avatar Fenny commented on May 1, 2024

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

from fiber.

schweigert avatar schweigert commented on May 1, 2024

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.

Fenny avatar Fenny commented on May 1, 2024

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

from fiber.

pjebs avatar pjebs commented on May 1, 2024

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

from fiber.

cdunn2001 avatar cdunn2001 commented on May 1, 2024

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

from fiber.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.