github.com/xmidt-org/webpa-common@v1.11.9/xhttp/gate/constructor_test.go (about)

     1  package gate
     2  
     3  import (
     4  	"net/http"
     5  	"net/http/httptest"
     6  	"testing"
     7  
     8  	"github.com/stretchr/testify/assert"
     9  	"github.com/stretchr/testify/require"
    10  )
    11  
    12  func testNewConstructorNilGate(t *testing.T) {
    13  	assert := assert.New(t)
    14  	assert.Panics(func() {
    15  		NewConstructor(nil)
    16  	})
    17  }
    18  
    19  func testNewConstructorDefault(t *testing.T, c func(http.Handler) http.Handler, g Interface) {
    20  	var (
    21  		assert  = assert.New(t)
    22  		require = require.New(t)
    23  
    24  		next = http.HandlerFunc(func(response http.ResponseWriter, _ *http.Request) {
    25  			response.WriteHeader(201)
    26  		})
    27  	)
    28  
    29  	require.NotNil(c)
    30  	require.NotNil(g)
    31  
    32  	decorated := c(next)
    33  	require.NotNil(decorated)
    34  
    35  	response := httptest.NewRecorder()
    36  	decorated.ServeHTTP(response, httptest.NewRequest("GET", "/", nil))
    37  	assert.Equal(201, response.Code)
    38  
    39  	g.Lower()
    40  	response = httptest.NewRecorder()
    41  	decorated.ServeHTTP(response, httptest.NewRequest("GET", "/", nil))
    42  	assert.Equal(http.StatusServiceUnavailable, response.Code)
    43  }
    44  
    45  func testNewConstructorCustomClosed(t *testing.T) {
    46  	var (
    47  		assert  = assert.New(t)
    48  		require = require.New(t)
    49  
    50  		closed = http.HandlerFunc(func(response http.ResponseWriter, _ *http.Request) {
    51  			response.Header().Set("X-Test", "foobar")
    52  			response.WriteHeader(599)
    53  		})
    54  
    55  		next = http.HandlerFunc(func(response http.ResponseWriter, _ *http.Request) {
    56  			response.WriteHeader(201)
    57  		})
    58  
    59  		g = New(true)
    60  		c = NewConstructor(g, WithClosedHandler(closed))
    61  	)
    62  
    63  	require.NotNil(c)
    64  
    65  	decorated := c(next)
    66  	require.NotNil(decorated)
    67  
    68  	response := httptest.NewRecorder()
    69  	decorated.ServeHTTP(response, httptest.NewRequest("GET", "/", nil))
    70  	assert.Equal(201, response.Code)
    71  	assert.Empty(response.Header())
    72  
    73  	g.Lower()
    74  	response = httptest.NewRecorder()
    75  	decorated.ServeHTTP(response, httptest.NewRequest("GET", "/", nil))
    76  	assert.Equal(599, response.Code)
    77  	assert.Equal("foobar", response.Header().Get("X-Test"))
    78  }
    79  
    80  func TestNewConstructor(t *testing.T) {
    81  	t.Run("NilGate", testNewConstructorNilGate)
    82  	t.Run("Default", func(t *testing.T) {
    83  		g := New(true)
    84  		testNewConstructorDefault(t, NewConstructor(g), g)
    85  
    86  		g = New(true)
    87  		testNewConstructorDefault(t, NewConstructor(g, WithClosedHandler(nil)), g)
    88  	})
    89  
    90  	t.Run("CustomClosed", testNewConstructorCustomClosed)
    91  }