github.com/msales/pkg/v3@v3.24.0/httpx/middleware/context_test.go (about)

     1  package middleware_test
     2  
     3  import (
     4  	"context"
     5  	"net/http"
     6  	"net/http/httptest"
     7  	"testing"
     8  	"time"
     9  
    10  	"github.com/msales/pkg/v3/httpx/middleware"
    11  	"github.com/stretchr/testify/assert"
    12  )
    13  
    14  func TestWithContext(t *testing.T) {
    15  	ctx := context.WithValue(context.Background(), "test", "test")
    16  
    17  	h := middleware.WithContext(ctx, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
    18  		assert.Equal(t, "test", r.Context().Value("test"))
    19  	}))
    20  
    21  	req, _ := http.NewRequest("GET", "/", nil)
    22  	resp := httptest.NewRecorder()
    23  
    24  	h.ServeHTTP(resp, req)
    25  }
    26  
    27  func TestWithContext_Value(t *testing.T) {
    28  	ctx := context.WithValue(context.Background(), "test", "test")
    29  	reqCtx := context.WithValue(context.Background(), "req", "req")
    30  
    31  	h := middleware.WithContext(ctx, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
    32  		assert.Equal(t, "test", r.Context().Value("test"))
    33  		assert.Equal(t, "req", r.Context().Value("req"))
    34  	}))
    35  
    36  	req, _ := http.NewRequest("GET", "/", nil)
    37  	resp := httptest.NewRecorder()
    38  
    39  	h.ServeHTTP(resp, req.WithContext(reqCtx))
    40  }
    41  
    42  func TestWithContext_WithDeadline(t *testing.T) {
    43  	ctx := context.WithValue(context.Background(), "test", "test")
    44  
    45  	deadline, err := time.Parse(time.RFC822Z, "11 Nov 19 17:32 -0700")
    46  	if err != nil {
    47  		panic(err)
    48  	}
    49  
    50  	reqCtx, cancel := context.WithDeadline(context.Background(), deadline)
    51  	defer cancel()
    52  
    53  	h := middleware.WithContext(ctx, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
    54  		assert.Equal(t, "test", r.Context().Value("test"))
    55  
    56  		reqDeadline, ok := r.Context().Deadline()
    57  		assert.True(t, ok)
    58  		assert.Equal(t, deadline, reqDeadline)
    59  	}))
    60  
    61  	req, _ := http.NewRequest("GET", "/", nil)
    62  	resp := httptest.NewRecorder()
    63  
    64  	h.ServeHTTP(resp, req.WithContext(reqCtx))
    65  }
    66  
    67  func TestWithContext_WithCancel(t *testing.T) {
    68  	ctx := context.WithValue(context.Background(), "test", "test")
    69  	reqCtx, cancel := context.WithCancel(context.Background())
    70  	defer cancel()
    71  
    72  	h := middleware.WithContext(ctx, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
    73  		assert.Equal(t, "test", r.Context().Value("test"))
    74  
    75  		reqDone := r.Context().Done()
    76  		assert.NotNil(t, reqDone)
    77  	}))
    78  
    79  	req, _ := http.NewRequest("GET", "/", nil)
    80  	resp := httptest.NewRecorder()
    81  
    82  	h.ServeHTTP(resp, req.WithContext(reqCtx))
    83  }