如何模拟 Fiber 上下文

huangapple go评论97阅读模式
英文:

go - How to mock fiber context

问题

我一直在尝试模拟一个fiber.Ctx,但是我无法使其工作,我一直得到这个错误:

--- FAIL: TestCheckHeaders (0.00s)
panic: runtime error: invalid memory address or nil pointer dereference [recovered]
panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x0 pc=0x12085f0]

我正在尝试测试的代码:

CheckHeaders.go

 package middleware

 import "github.com/gofiber/fiber/v2"

 func CheckHeaders(c *fiber.Ctx) error {
	headers := c.GetReqHeaders()
	if headers["headerValue"] == "true"{
		return c.Next()
	} else {
		return c.SendStatus(401)

	}
 }

CheckHeaders_test.go

 package middleware

 import (
	"testing"
	"github.com/gofiber/fiber/v2"
 )

 func TestCheckHeaders(t *testing.T) {
	type args struct {
		c *fiber.Ctx
	}
	fiberContext := fiber.Ctx{}

	tests := []struct {
		name    string
		args    args
		wantErr bool
	}{
		{name: "test 1",
			args:    args{c: &fiberContext},
			wantErr: true,
		},
	}
	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			if err := CheckHeaders(tt.args.c); (err != nil) != tt.wantErr {
				t.Errorf("CheckHeaders() error = %v, wantErr %v", err,tt.wantErr)
			}
		})
	}
   }
英文:

I've been trying to mock a fiber.Ctx but I have not been able to make it work I have been getting this error:

>--- FAIL: TestCheckHeaders (0.00s)
panic: runtime error: invalid memory address or nil pointer dereference [recovered]
panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x0 pc=0x12085f0]

The code that I am trying to test:

CheckHeaders.go

 package middleware

 import "github.com/gofiber/fiber/v2"

 func CheckHeaders(c *fiber.Ctx) error {
	headers := c.GetReqHeaders()
	if headers["headerValue"] == "true"{
		return c.Next()
	} else {
		return c.SendStatus(401)

	}
 }

CheckHeaders_test.go

 package middleware

 import (
	"testing"
	"github.com/gofiber/fiber/v2"
 )

 func TestCheckHeaders(t *testing.T) {
	type args struct {
		c *fiber.Ctx
	}
	fiberContext := fiber.Ctx{}

	tests := []struct {
		name    string
		args    args
		wantErr bool
	}{
		{name: "test 1",
			args:    args{c: &fiberContext},
			wantErr: true,
		},
	}
	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			if err := CheckHeaders(tt.args.c); (err != nil) != tt.wantErr {
				t.Errorf("CheckHeaders() error = %v, wantErr %v", err,tt.wantErr)
			}
		})
	}
   }

答案1

得分: 2

这是我设置一个新的上下文以供测试操作的方式。希望对你有所帮助。

app := fiber.New()
c := app.AcquireCtx(&fasthttp.RequestCtx{})

c 将是一个全新的 *fiber.Ctx

英文:

This was how I set up a new context that I could manipulate for testing. Hope it helps.

app := fiber.New()
c := app.AcquireCtx(&fasthttp.RequestCtx{})

c will be a fresh *fiber.Ctx.

答案2

得分: 0

你可以使用fiber.Ctx结构体中需要使用的方法创建自己的接口,并为该接口创建模拟。

然后,你可以将fiber.Ctx处理为例如Ctxer接口的实现,这样你就可以使用https://github.com/golang/mock来模拟它并在测试中使用模拟。

代码示例如下:

type Ctxer interface {
    GetReqHeaders() map[string]string
    Next() (err error)
    SendStatus(status int) error
}

func CheckHeaders(c Ctxer) error {
    headers := c.GetReqHeaders()
    if headers["headerValue"] == "true" {
        return c.Next()
    } else {
        return c.SendStatus(401)
    }
}

以上是要翻译的内容。

英文:

You can create your own interface with methods you need to use from fiber.Ctx struct and make mock for the interface.

After that you are going to handle fiber.Ctx as implementation of for example Ctxer interface so you will be able to mock it using https://github.com/golang/mock and use the mock in your tests.

It's gonna look like this

type Ctxer interface {
	GetReqHeaders() map[string]string
	Next() (err error)
	SendStatus(status int) error
}

func CheckHeaders(c Ctxer) error {
	headers := c.GetReqHeaders()
	if headers["headerValue"] == "true" {
		return c.Next()
	} else {
		return c.SendStatus(401)
	}
}

huangapple
  • 本文由 发表于 2022年12月1日 15:39:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/74638385.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定