github.com/jxskiss/gopkg@v0.17.3/easy/http_test.go (about)

     1  package easy
     2  
     3  import (
     4  	"bytes"
     5  	"github.com/stretchr/testify/assert"
     6  	"github.com/stretchr/testify/require"
     7  	"io/ioutil"
     8  	"net/http"
     9  	"net/http/httptest"
    10  	"net/http/httputil"
    11  	"strings"
    12  	"testing"
    13  	"time"
    14  )
    15  
    16  func TestSingleJoin(t *testing.T) {
    17  	text := []string{"a", "b..", "..c"}
    18  	got := SingleJoin("..", text...)
    19  	want := "a..b..c"
    20  	assert.Equal(t, want, got)
    21  }
    22  
    23  func TestSlashJoin(t *testing.T) {
    24  	got0 := SlashJoin()
    25  	assert.Equal(t, "", got0)
    26  
    27  	path1 := []string{"/a", "b", "c.png"}
    28  	want1 := "/a/b/c.png"
    29  	got1 := SlashJoin(path1...)
    30  	assert.Equal(t, want1, got1)
    31  
    32  	path2 := []string{"/a/", "b/", "/c.png"}
    33  	want2 := "/a/b/c.png"
    34  	got2 := SlashJoin(path2...)
    35  	assert.Equal(t, want2, got2)
    36  }
    37  
    38  type testObject struct {
    39  	A int    `xml:"a" json:"a"`
    40  	B string `xml:"b" json:"b"`
    41  }
    42  
    43  func TestJSONToReader(t *testing.T) {
    44  	data := testObject{
    45  		A: 1,
    46  		B: "2",
    47  	}
    48  	r, err := JSONToReader(data)
    49  	require.Nil(t, err)
    50  
    51  	buf, _ := ioutil.ReadAll(r)
    52  	want := []byte(`{"a":1,"b":"2"}`)
    53  	assert.Equal(t, want, buf)
    54  }
    55  
    56  func TestDecodeJSON(t *testing.T) {
    57  	var data testObject
    58  	r := bytes.NewBufferString(`{"a":1,"b":"2"}`)
    59  	err := DecodeJSON(r, &data)
    60  	require.Nil(t, err)
    61  
    62  	want := testObject{A: 1, B: "2"}
    63  	assert.Equal(t, want, data)
    64  }
    65  
    66  func TestXMLToReader(t *testing.T) {
    67  	var data = testObject{
    68  		A: 123,
    69  		B: "456",
    70  	}
    71  	r, err := XMLToReader(data)
    72  	require.Nil(t, err)
    73  
    74  	buf, _ := ioutil.ReadAll(r)
    75  	want := []byte(`<testObject><a>123</a><b>456</b></testObject>`)
    76  	assert.Equal(t, want, buf)
    77  }
    78  
    79  func TestDecodeXML(t *testing.T) {
    80  	var data testObject
    81  	r := bytes.NewBufferString(`<testObject><a>123</a><b>456</b></testObject>`)
    82  	err := DecodeXML(r, &data)
    83  	require.Nil(t, err)
    84  
    85  	want := testObject{A: 123, B: "456"}
    86  	assert.Equal(t, want, data)
    87  }
    88  
    89  func BenchmarkSlashJoin(b *testing.B) {
    90  	path := []string{"/a", "b", "c.png"}
    91  	for i := 0; i < b.N; i++ {
    92  		_ = SlashJoin(path...)
    93  	}
    94  }
    95  
    96  func TestDoRequest(t *testing.T) {
    97  	s := httptest.NewServer(http.HandlerFunc(testHttpHandler))
    98  	defer s.Close()
    99  	data := "test DoRequest"
   100  	req, _ := http.NewRequest("POST", s.URL, strings.NewReader(data))
   101  
   102  	var respText []byte
   103  	var status int
   104  	var err error
   105  
   106  	logbuf := CopyStdLog(func() {
   107  		respText, status, err = DoRequest(&Request{
   108  			Req:          req,
   109  			Timeout:      time.Second,
   110  			DumpRequest:  true,
   111  			DumpResponse: true,
   112  		})
   113  	})
   114  	assert.Nil(t, err)
   115  	assert.Equal(t, 200, status)
   116  	assert.Contains(t, string(respText), data)
   117  	count := bytes.Count(logbuf, []byte(data))
   118  	assert.Equal(t, 2, count)
   119  }
   120  
   121  func testHttpHandler(w http.ResponseWriter, r *http.Request) {
   122  	dump, err := httputil.DumpRequest(r, true)
   123  	if err != nil {
   124  		panic(err)
   125  	}
   126  	w.Write(dump)
   127  }