Skip to content
This repository has been archived by the owner on Nov 2, 2023. It is now read-only.

Commit

Permalink
sdk/middleware/sqgin: context data flow test case
Browse files Browse the repository at this point in the history
  • Loading branch information
Julio Guerra committed Sep 30, 2020
1 parent 4cf9527 commit 84ce914
Showing 1 changed file with 48 additions and 1 deletion.
49 changes: 48 additions & 1 deletion sdk/middleware/sqgin/gin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package sqgin

import (
"context"
"net/http"
"net/http/httptest"
"testing"
Expand Down Expand Up @@ -91,7 +92,7 @@ func TestMiddleware(t *testing.T) {
})

// Test how the control flows between middleware and handler functions
t.Run("control flow", func(t *testing.T) {
t.Run("data and control flow", func(t *testing.T) {
middlewareResponseBody := testlib.RandUTF8String(4096)
middlewareResponseStatus := 433
handlerResponseBody := testlib.RandUTF8String(4096)
Expand Down Expand Up @@ -124,6 +125,11 @@ func TestMiddleware(t *testing.T) {
handler func(*gin.Context)
test func(t *testing.T, rec *httptest.ResponseRecorder)
}{
//
// Control flow tests
// When an handlers, including middlewares, block.
//

{
name: "sqreen first/next middleware aborts before the handler",
middlewares: []gin.HandlerFunc{
Expand Down Expand Up @@ -292,6 +298,47 @@ func TestMiddleware(t *testing.T) {
require.Equal(t, middlewareResponseBody, rec.Body.String())
},
},

//
// Context data flow tests
//
{
name: "middleware1, sqreen, middleware2, handler",
middlewares: []gin.HandlerFunc{
func(c *gin.Context) {
c.Set("m10", "v10")
c.Request = c.Request.WithContext(context.WithValue(c.Request.Context(), "m11", "v11"))
},
middleware(tc.agent),
func(c *gin.Context) {
c.Set("m20", "v20")
c.Request = c.Request.WithContext(context.WithValue(c.Request.Context(), "m21", "v21"))
},
},
handler: func(c *gin.Context) {
// From Gin's context
if v, ok := c.Value("m10").(string); !ok || v != "v10" {
panic("couldn't get the context value m10")
}
if v, ok := c.Value("m20").(string); !ok || v != "v20" {
panic("couldn't get the context value m20")
}

// From the request context
reqCtx := c.Request.Context()
if v, ok := reqCtx.Value("m11").(string); !ok || v != "v11" {
panic("couldn't get the context value m11")
}
if v, ok := reqCtx.Value("m21").(string); !ok || v != "v21" {
panic("couldn't get the context value m21")
}

c.Status(http.StatusOK)
},
test: func(t *testing.T, rec *httptest.ResponseRecorder) {
require.Equal(t, http.StatusOK, rec.Code)
},
},
} {
tc := tc
t.Run(tc.name, func(t *testing.T) {
Expand Down

0 comments on commit 84ce914

Please sign in to comment.