go.sdls.io/sin@v0.0.9/pkg/sin/utils_test.go (about)

     1  // Copyright 2014 Manu Martinez-Almeida.  All rights reserved.
     2  // Use of this source code is governed by a MIT style
     3  // license that can be found in the LICENSE file.
     4  
     5  package sin
     6  
     7  import (
     8  	"bytes"
     9  	"encoding/xml"
    10  	"fmt"
    11  	"net/http"
    12  	"testing"
    13  
    14  	"github.com/stretchr/testify/assert"
    15  )
    16  
    17  type testStruct struct {
    18  	T *testing.T
    19  }
    20  
    21  func (t *testStruct) ServeHTTP(w http.ResponseWriter, req *http.Request) {
    22  	assert.Equal(t.T, "POST", req.Method)
    23  	assert.Equal(t.T, "/path", req.URL.Path)
    24  	w.WriteHeader(http.StatusInternalServerError)
    25  	_, _ = fmt.Fprint(w, "hello")
    26  }
    27  
    28  func TestWrap(t *testing.T) {
    29  	router := New()
    30  	router.POST("/path", WrapH(&testStruct{t}))
    31  	router.GET("/path2", WrapF(func(w http.ResponseWriter, req *http.Request) {
    32  		assert.Equal(t, "GET", req.Method)
    33  		assert.Equal(t, "/path2", req.URL.Path)
    34  		w.WriteHeader(http.StatusBadRequest)
    35  		_, _ = fmt.Fprint(w, "hola!")
    36  	}))
    37  
    38  	w := performRequest(router, "POST", "/path")
    39  	assert.Equal(t, http.StatusInternalServerError, w.Code)
    40  	assert.Equal(t, "hello", w.Body.String())
    41  
    42  	w = performRequest(router, "GET", "/path2")
    43  	assert.Equal(t, http.StatusBadRequest, w.Code)
    44  	assert.Equal(t, "hola!", w.Body.String())
    45  }
    46  
    47  func TestLastChar(t *testing.T) {
    48  	assert.Equal(t, uint8('a'), lastChar("hola"))
    49  	assert.Equal(t, uint8('s'), lastChar("adios"))
    50  	assert.Panics(t, func() { lastChar("") })
    51  }
    52  
    53  func TestFilterFlags(t *testing.T) {
    54  	result := filterFlags("text/html ")
    55  	assert.Equal(t, "text/html", result)
    56  
    57  	result = filterFlags("text/html;")
    58  	assert.Equal(t, "text/html", result)
    59  }
    60  
    61  func TestFunctionName(t *testing.T) {
    62  	assert.Regexp(t, `^(.*/vendor/)?go.sdls.io/sin/pkg/sin.somefunction$`, nameOfFunction(somefunction))
    63  }
    64  
    65  func somefunction() {
    66  	// this empty function is used by TestFunctionName()
    67  }
    68  
    69  func TestJoinPaths(t *testing.T) {
    70  	assert.Equal(t, "", joinPaths("", ""))
    71  	assert.Equal(t, "/", joinPaths("", "/"))
    72  	assert.Equal(t, "/a", joinPaths("/a", ""))
    73  	assert.Equal(t, "/a/", joinPaths("/a/", ""))
    74  	assert.Equal(t, "/a/", joinPaths("/a/", "/"))
    75  	assert.Equal(t, "/a/", joinPaths("/a", "/"))
    76  	assert.Equal(t, "/a/hola", joinPaths("/a", "/hola"))
    77  	assert.Equal(t, "/a/hola", joinPaths("/a/", "/hola"))
    78  	assert.Equal(t, "/a/hola/", joinPaths("/a/", "/hola/"))
    79  	assert.Equal(t, "/a/hola/", joinPaths("/a/", "/hola//"))
    80  }
    81  
    82  func TestMarshalXMLforH(t *testing.T) {
    83  	h := H{
    84  		"": "test",
    85  	}
    86  	var b bytes.Buffer
    87  	enc := xml.NewEncoder(&b)
    88  	var x xml.StartElement
    89  	e := h.MarshalXML(enc, x)
    90  	assert.Error(t, e)
    91  }