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

     1  package xhttp
     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 testStaticHeaders(t *testing.T, extra, expected http.Header) {
    13  	var (
    14  		assert  = assert.New(t)
    15  		require = require.New(t)
    16  
    17  		response = httptest.NewRecorder()
    18  		request  = httptest.NewRequest("GET", "/", nil)
    19  
    20  		decoratedCalled = false
    21  		next            = http.HandlerFunc(func(response http.ResponseWriter, request *http.Request) {
    22  			decoratedCalled = true
    23  			assert.Equal(len(expected), len(response.Header()))
    24  			for k, v := range expected {
    25  				assert.Equal(v, response.Header()[k])
    26  			}
    27  		})
    28  
    29  		constructor = StaticHeaders(extra)
    30  	)
    31  
    32  	require.NotNil(constructor)
    33  	decorated := constructor(next)
    34  
    35  	decorated.ServeHTTP(response, request)
    36  	assert.True(decoratedCalled)
    37  }
    38  
    39  func TestStaticHeaders(t *testing.T) {
    40  	t.Run("Nil", func(t *testing.T) { testStaticHeaders(t, nil, nil) })
    41  	t.Run("Empty", func(t *testing.T) { testStaticHeaders(t, http.Header{}, nil) })
    42  	t.Run("Several", func(t *testing.T) {
    43  		testStaticHeaders(
    44  			t,
    45  			http.Header{
    46  				"Content-Type": {"application/json"},
    47  				"x-something":  {"value1", "value2"},
    48  				"eMPtY":        {},
    49  			},
    50  			http.Header{
    51  				"Content-Type": {"application/json"},
    52  				"X-Something":  {"value1", "value2"},
    53  				"Empty":        {},
    54  			},
    55  		)
    56  	})
    57  }