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

Why net.Addr is fiber.testAddr, not *net.TCPAddr?🤗 #1574

Closed
fiecato opened this issue Oct 14, 2021 · 5 comments · Fixed by #1784
Closed

Why net.Addr is fiber.testAddr, not *net.TCPAddr?🤗 #1574

fiecato opened this issue Oct 14, 2021 · 5 comments · Fixed by #1784

Comments

@fiecato
Copy link

fiecato commented Oct 14, 2021

Question description
I use app.Test for unit testing, but panic: interface conversion: net.Addr is fiber.testAddr, not *net.TCPAddr is reported

Code snippet Optional

initialize.go of InitTrancer

func InitTracer() (closer io.Closer) {
	global.Tracer, closer, _ = ot.NewJaegerTracer("fxs-api", global.Jaeger.Agent)
	return closer
}

router.go

package router

import (
	"e.coding.net/theguyver/fxs/fxs-api/global"
	"e.coding.net/theguyver/fxs/fxs-api/internal/handler/auth"
	"e.coding.net/theguyver/fxs/fxs-api/internal/handler/dealers"
	"e.coding.net/theguyver/guyver/internal-services/gotools/middleware"
	"github.com/gofiber/fiber/v2"
	"github.com/gofiber/fiber/v2/middleware/pprof"
	"github.com/gofiber/fiber/v2/middleware/recover"
)

func SetupRouter(app *fiber.App) {
        recover.ConfigDefault.EnableStackTrace = true
	app.Use(recover.New())
	app.Use(pprof.New())
	app.Use(middleware.AccessLog())
	// Tracing
	app.Use(middleware.JeagerTracing(global.Tracer))
	midRouter := middleware.ValidateToken(global.TokenService, global.AutoExpireMinutes)

	V0AdminRouter := app.Group("fxs/v0/admin")

        V0AdminRouter.Get("/dealers", midRouter, dealers.ListDealer)
}

middlerware.go

// AccessLog record request code
func AccessLog() fiber.Handler {
	return func(c *fiber.Ctx) error {
		startTime := time.Now()
		chainErr := c.Next()
		if chainErr != nil {
			FiberErrorHandler(c, chainErr)
		}
		endTime := time.Now()
		zap.L().Info(c.Path(),
			zap.String("method", c.Method()),
			zap.String("ip", c.IP()),
			zap.String("port", c.Port()),
			zap.Int("status", c.Response().StatusCode()),
			zap.String("user-agent", string(c.Request().Header.UserAgent())),
			zap.Any("errors", chainErr),
			zap.Duration("cost", endTime.Sub(startTime).Round(time.Millisecond)),
		)
		return nil
	}

}
package tests

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

var app = &fiber.App{}

func init() {
        closer := initialize.InitTracer()
	defer closer.Close()
        
        app = fiber.New(fiber.Config{
		Prefork:       false,
		CaseSensitive: true,
		StrictRouting: true,
		AppName:       "fxs-pai",
		ErrorHandler:  utils.GlobalErrorHandler,
	})

	router.SetupRouter(app)
}

func TestListDealer(t *testing.T) {
	tests := []struct {
		name    string
		route   string
		wantErr bool
	}{
		{
			name:    "test list dealer with only pageSize",
			route:   "/fxs/v0/admin/dealers?pageSize=10",
			wantErr: false,
		},
	}
	for _, tt := range tests {
		req := httptest.NewRequest(http.MethodGet, tt.route, nil)
		resp, err := app.Test(req, -1)
        if err != nil {
            t.Errorf("%s of %s failed:%v", tt.name, tt.route, err)
        }
        if (resp.StatusCode != http.StatusOK) != tt.wantErr {
            t.Errorf("%s response failed: %v", tt.name, resp)
        }
	}
}

How should I deal with this situation?

@welcome
Copy link

welcome bot commented Oct 14, 2021

Thanks for opening your first issue here! 🎉 Be sure to follow the issue template! If you need help or want to chat with us, join us on Discord https://gofiber.io/discord

@Vytek
Copy link

Vytek commented Feb 17, 2022

Hello, I have the same error. How can I fix it??

@ReneWerner87
Copy link
Member

@Vytek @fiecato
this error is not easy to recreate

have tried it with the code snippets, however, I see no error

would it be possible for someone to provide a minimal main and test file where this problem occurs?

with all the resources that are needed

@Vytek
Copy link

Vytek commented Feb 18, 2022

Hi @ReneWerner87,
I recreate the error and develop a little repo with all.

This is the public repo with all the code:
https://github.com/Vytek/unittesterrorgofiber

We test the error on:

Distributor ID: Ubuntu
Description: Ubuntu 20.04.3 LTS
Release: 20.04
Codename: focal

Go Version:

go version go1.17.6 linux/amd64

Thank you very much!

@ReneWerner87
Copy link
Member

thanks for the example, now i see the problem and will work on a fix

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

Successfully merging a pull request may close this issue.

3 participants