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

     1  // Copyright 2017 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  	"bufio"
     9  	"crypto/tls"
    10  	"fmt"
    11  	"io/ioutil"
    12  	"net"
    13  	"net/http"
    14  	"net/http/httptest"
    15  	"sync"
    16  	"testing"
    17  	"time"
    18  
    19  	"github.com/stretchr/testify/assert"
    20  )
    21  
    22  func testRequest(t *testing.T, url string) {
    23  	tr := &http.Transport{
    24  		// #nosec G402
    25  		TLSClientConfig: &tls.Config{
    26  			InsecureSkipVerify: true,
    27  		},
    28  	}
    29  	client := &http.Client{Transport: tr}
    30  
    31  	resp, err := client.Get(url)
    32  	assert.NoError(t, err)
    33  	defer resp.Body.Close()
    34  
    35  	body, ioerr := ioutil.ReadAll(resp.Body)
    36  	assert.NoError(t, ioerr)
    37  	assert.Equal(t, "it worked", string(body), "resp body should match")
    38  	assert.Equal(t, "200 OK", resp.Status, "should get a 200")
    39  }
    40  
    41  func TestRunWithPort(t *testing.T) {
    42  	router := New()
    43  	go func() {
    44  		router.GET("/example", func(c *Context) { c.String(http.StatusOK, "it worked") })
    45  		assert.NoError(t, router.Run(":5150"))
    46  	}()
    47  	// have to wait for the goroutine to start and run the server
    48  	// otherwise the main thread will complete
    49  	time.Sleep(5 * time.Millisecond)
    50  
    51  	assert.Error(t, router.Run(":5150"))
    52  	testRequest(t, "http://localhost:5150/example")
    53  }
    54  
    55  func TestListener(t *testing.T) {
    56  	router := New()
    57  	addr, err := net.ResolveTCPAddr("tcp", "localhost:0")
    58  	assert.NoError(t, err)
    59  	listener, err := net.ListenTCP("tcp", addr)
    60  	assert.NoError(t, err)
    61  	go func() {
    62  		router.GET("/example", func(c *Context) { c.String(http.StatusOK, "it worked") })
    63  		assert.NoError(t, router.RunListener(listener))
    64  	}()
    65  	// have to wait for the goroutine to start and run the server
    66  	// otherwise the main thread will complete
    67  	time.Sleep(5 * time.Millisecond)
    68  
    69  	c, err := net.Dial("tcp", listener.Addr().String())
    70  	assert.NoError(t, err)
    71  
    72  	_, _ = fmt.Fprintf(c, "GET /example HTTP/1.0\r\n\r\n")
    73  	scanner := bufio.NewScanner(c)
    74  	var response string
    75  	for scanner.Scan() {
    76  		response += scanner.Text()
    77  	}
    78  	assert.Contains(t, response, "HTTP/1.0 200", "should get a 200")
    79  	assert.Contains(t, response, "it worked", "resp body should match")
    80  }
    81  
    82  func TestBadListener(t *testing.T) {
    83  	router := New()
    84  	addr, err := net.ResolveTCPAddr("tcp", "localhost:10086")
    85  	assert.NoError(t, err)
    86  	listener, err := net.ListenTCP("tcp", addr)
    87  	assert.NoError(t, err)
    88  	_ = listener.Close()
    89  	assert.Error(t, router.RunListener(listener))
    90  }
    91  
    92  func TestWithHttptestWithAutoSelectedPort(t *testing.T) {
    93  	router := New()
    94  	router.GET("/example", func(c *Context) { c.String(http.StatusOK, "it worked") })
    95  
    96  	ts := httptest.NewServer(router)
    97  	defer ts.Close()
    98  
    99  	testRequest(t, ts.URL+"/example")
   100  }
   101  
   102  func TestConcurrentHandleContext(t *testing.T) {
   103  	router := New()
   104  	router.GET("/", func(c *Context) {
   105  		c.Request.URL.Path = "/example"
   106  		router.HandleContext(c)
   107  	})
   108  	router.GET("/example", func(c *Context) { c.String(http.StatusOK, "it worked") })
   109  
   110  	var wg sync.WaitGroup
   111  	iterations := 200
   112  	wg.Add(iterations)
   113  	for i := 0; i < iterations; i++ {
   114  		go func() {
   115  			testGetRequestHandler(t, router, "/")
   116  			wg.Done()
   117  		}()
   118  	}
   119  	wg.Wait()
   120  }
   121  
   122  func testGetRequestHandler(t *testing.T, h http.Handler, url string) {
   123  	req, err := http.NewRequest(http.MethodGet, url, nil)
   124  	assert.NoError(t, err)
   125  
   126  	w := httptest.NewRecorder()
   127  	h.ServeHTTP(w, req)
   128  
   129  	assert.Equal(t, "it worked", w.Body.String(), "resp body should match")
   130  	assert.Equal(t, 200, w.Code, "should get a 200")
   131  }