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

     1  package xhttp
     2  
     3  import (
     4  	"net/http"
     5  	"net/http/httptest"
     6  	"strconv"
     7  	"testing"
     8  
     9  	"github.com/stretchr/testify/assert"
    10  )
    11  
    12  func testConstant(t *testing.T, c Constant) {
    13  	var (
    14  		assert   = assert.New(t)
    15  		response = httptest.NewRecorder()
    16  		request  = httptest.NewRequest("GET", "/", nil)
    17  	)
    18  
    19  	c.ServeHTTP(response, request)
    20  	assert.Equal(c.Code, response.Code)
    21  	if c.Header != nil {
    22  		assert.Equal(c.Header, response.Header())
    23  	} else {
    24  		assert.Empty(response.Header())
    25  	}
    26  
    27  	assert.Equal(c.Body, response.Body.Bytes())
    28  }
    29  
    30  func TestConstant(t *testing.T) {
    31  	testData := []Constant{
    32  		{Code: http.StatusNotFound},
    33  		{Code: http.StatusServiceUnavailable, Header: http.Header{"Content-Type": []string{"application/json"}}, Body: []byte(`{"message": "oh noes!"}`)},
    34  	}
    35  
    36  	for i, c := range testData {
    37  		t.Run(strconv.Itoa(i), func(t *testing.T) {
    38  			testConstant(t, c)
    39  		})
    40  	}
    41  }