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

Race conditions when using github.com/Kong/go-pdk/test #213

Open
stefan-knowles opened this issue Sep 25, 2024 · 0 comments
Open

Race conditions when using github.com/Kong/go-pdk/test #213

stefan-knowles opened this issue Sep 25, 2024 · 0 comments

Comments

@stefan-knowles
Copy link

stefan-knowles commented Sep 25, 2024

Unfortunately i intermittently get the following error when running tests
panic: Fail in goroutine after TestRejectsRequestWithEmptyToken has completed

// main.go
// [...]
type requestContext struct {
	kong      *pdk.PDK
	startTime time.Time
}

func (conf *Config) Access(kong *pdk.PDK) {
	tokenString, err := kong.Request.GetHeader("Authorization")
	if err != nil {
		conf.setExitStatus(requestCtx, 401, "Error reading 'Authorization' header", "error", err)
		return
	}
	if tokenString == "" {
		conf.setExitStatus(requestCtx, 401, "Invalid 'Authorization' header")
		return
	}
}

func (conf *Config) setExitStatus(ctx *requestContext, status int, msg string, msgArgs ...any) {
	slog.Error(msg, msgArgs...)
	slog.Info("Request complete", "elapseTime", time.Now().Sub(ctx.startTime))
	ctx.kong.Response.ExitStatus(status)
}
//main_test.go
// [...]
func TestRejectsRequestWithEmptyToken(t *testing.T) {
	RegisterTestingT(t)
    env, err := test.New(t, test.Request{
        Method:  "GET",
        Url:     "http://example.com/request-path",
        Headers: map[string][]string{"Authorization": {""},},
    })
    Expect(err).NotTo(HaveOccurred(), "failed execute test request: %s\n", err)

    env.DoHttps(&Config{})
	Expect(env.ClientRes.Status).To(Equal(401))
}
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

1 participant