github.com/hellobchain/third_party@v0.0.0-20230331131523-deb0478a2e52/gin/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 gin 6 7 import ( 8 "bufio" 9 "fmt" 10 "github.com/hellobchain/newcryptosm/http" 11 "github.com/hellobchain/newcryptosm/http/httptest" 12 "github.com/hellobchain/newcryptosm/tls" 13 "html/template" 14 "io/ioutil" 15 "net" 16 "os" 17 "path/filepath" 18 "sync" 19 "testing" 20 "time" 21 22 "github.com/stretchr/testify/assert" 23 ) 24 25 func testRequest(t *testing.T, url string) { 26 tr := &http.Transport{ 27 TLSClientConfig: &tls.Config{ 28 InsecureSkipVerify: true, 29 }, 30 } 31 client := &http.Client{Transport: tr} 32 33 resp, err := client.Get(url) 34 assert.NoError(t, err) 35 defer resp.Body.Close() 36 37 body, ioerr := ioutil.ReadAll(resp.Body) 38 assert.NoError(t, ioerr) 39 assert.Equal(t, "it worked", string(body), "resp body should match") 40 assert.Equal(t, "200 OK", resp.Status, "should get a 200") 41 } 42 43 func TestRunEmpty(t *testing.T) { 44 os.Setenv("PORT", "") 45 router := New() 46 go func() { 47 router.GET("/example", func(c *Context) { c.String(http.StatusOK, "it worked") }) 48 assert.NoError(t, router.Run()) 49 }() 50 // have to wait for the goroutine to start and run the server 51 // otherwise the main thread will complete 52 time.Sleep(5 * time.Millisecond) 53 54 assert.Error(t, router.Run(":8080")) 55 testRequest(t, "http://localhost:8080/example") 56 } 57 58 func TestTrustedCIDRsForRun(t *testing.T) { 59 os.Setenv("PORT", "") 60 router := New() 61 router.TrustedProxies = []string{"hello/world"} 62 assert.Error(t, router.Run(":8080")) 63 } 64 65 func TestRunTLS(t *testing.T) { 66 router := New() 67 go func() { 68 router.GET("/example", func(c *Context) { c.String(http.StatusOK, "it worked") }) 69 70 assert.NoError(t, router.RunTLS(":8443", "./testdata/certificate/cert.pem", "./testdata/certificate/key.pem")) 71 }() 72 73 // have to wait for the goroutine to start and run the server 74 // otherwise the main thread will complete 75 time.Sleep(5 * time.Millisecond) 76 77 assert.Error(t, router.RunTLS(":8443", "./testdata/certificate/cert.pem", "./testdata/certificate/key.pem")) 78 testRequest(t, "https://localhost:8443/example") 79 } 80 81 func TestPusher(t *testing.T) { 82 var html = template.Must(template.New("https").Parse(` 83 <html> 84 <head> 85 <title>Https Test</title> 86 <script src="/assets/app.js"></script> 87 </head> 88 <body> 89 <h1 style="color:red;">Welcome, Ginner!</h1> 90 </body> 91 </html> 92 `)) 93 94 router := New() 95 router.Static("./assets", "./assets") 96 router.SetHTMLTemplate(html) 97 98 go func() { 99 router.GET("/pusher", func(c *Context) { 100 if pusher := c.Writer.Pusher(); pusher != nil { 101 err := pusher.Push("/assets/app.js", nil) 102 assert.NoError(t, err) 103 } 104 c.String(http.StatusOK, "it worked") 105 }) 106 107 assert.NoError(t, router.RunTLS(":8449", "./testdata/certificate/cert.pem", "./testdata/certificate/key.pem")) 108 }() 109 110 // have to wait for the goroutine to start and run the server 111 // otherwise the main thread will complete 112 time.Sleep(5 * time.Millisecond) 113 114 assert.Error(t, router.RunTLS(":8449", "./testdata/certificate/cert.pem", "./testdata/certificate/key.pem")) 115 testRequest(t, "https://localhost:8449/pusher") 116 } 117 118 func TestRunEmptyWithEnv(t *testing.T) { 119 os.Setenv("PORT", "3123") 120 router := New() 121 go func() { 122 router.GET("/example", func(c *Context) { c.String(http.StatusOK, "it worked") }) 123 assert.NoError(t, router.Run()) 124 }() 125 // have to wait for the goroutine to start and run the server 126 // otherwise the main thread will complete 127 time.Sleep(5 * time.Millisecond) 128 129 assert.Error(t, router.Run(":3123")) 130 testRequest(t, "http://localhost:3123/example") 131 } 132 133 func TestRunTooMuchParams(t *testing.T) { 134 router := New() 135 assert.Panics(t, func() { 136 assert.NoError(t, router.Run("2", "2")) 137 }) 138 } 139 140 func TestRunWithPort(t *testing.T) { 141 router := New() 142 go func() { 143 router.GET("/example", func(c *Context) { c.String(http.StatusOK, "it worked") }) 144 assert.NoError(t, router.Run(":5150")) 145 }() 146 // have to wait for the goroutine to start and run the server 147 // otherwise the main thread will complete 148 time.Sleep(5 * time.Millisecond) 149 150 assert.Error(t, router.Run(":5150")) 151 testRequest(t, "http://localhost:5150/example") 152 } 153 154 func TestUnixSocket(t *testing.T) { 155 router := New() 156 157 unixTestSocket := filepath.Join(os.TempDir(), "unix_unit_test") 158 159 defer os.Remove(unixTestSocket) 160 161 go func() { 162 router.GET("/example", func(c *Context) { c.String(http.StatusOK, "it worked") }) 163 assert.NoError(t, router.RunUnix(unixTestSocket)) 164 }() 165 // have to wait for the goroutine to start and run the server 166 // otherwise the main thread will complete 167 time.Sleep(5 * time.Millisecond) 168 169 c, err := net.Dial("unix", unixTestSocket) 170 assert.NoError(t, err) 171 172 fmt.Fprint(c, "GET /example HTTP/1.0\r\n\r\n") 173 scanner := bufio.NewScanner(c) 174 var response string 175 for scanner.Scan() { 176 response += scanner.Text() 177 } 178 assert.Contains(t, response, "HTTP/1.0 200", "should get a 200") 179 assert.Contains(t, response, "it worked", "resp body should match") 180 } 181 182 func TestBadUnixSocket(t *testing.T) { 183 router := New() 184 assert.Error(t, router.RunUnix("#/tmp/unix_unit_test")) 185 } 186 187 func TestFileDescriptor(t *testing.T) { 188 router := New() 189 190 addr, err := net.ResolveTCPAddr("tcp", "localhost:0") 191 assert.NoError(t, err) 192 listener, err := net.ListenTCP("tcp", addr) 193 assert.NoError(t, err) 194 socketFile, err := listener.File() 195 assert.NoError(t, err) 196 197 go func() { 198 router.GET("/example", func(c *Context) { c.String(http.StatusOK, "it worked") }) 199 assert.NoError(t, router.RunFd(int(socketFile.Fd()))) 200 }() 201 // have to wait for the goroutine to start and run the server 202 // otherwise the main thread will complete 203 time.Sleep(5 * time.Millisecond) 204 205 c, err := net.Dial("tcp", listener.Addr().String()) 206 assert.NoError(t, err) 207 208 fmt.Fprintf(c, "GET /example HTTP/1.0\r\n\r\n") 209 scanner := bufio.NewScanner(c) 210 var response string 211 for scanner.Scan() { 212 response += scanner.Text() 213 } 214 assert.Contains(t, response, "HTTP/1.0 200", "should get a 200") 215 assert.Contains(t, response, "it worked", "resp body should match") 216 } 217 218 func TestBadFileDescriptor(t *testing.T) { 219 router := New() 220 assert.Error(t, router.RunFd(0)) 221 } 222 223 func TestListener(t *testing.T) { 224 router := New() 225 addr, err := net.ResolveTCPAddr("tcp", "localhost:0") 226 assert.NoError(t, err) 227 listener, err := net.ListenTCP("tcp", addr) 228 assert.NoError(t, err) 229 go func() { 230 router.GET("/example", func(c *Context) { c.String(http.StatusOK, "it worked") }) 231 assert.NoError(t, router.RunListener(listener)) 232 }() 233 // have to wait for the goroutine to start and run the server 234 // otherwise the main thread will complete 235 time.Sleep(5 * time.Millisecond) 236 237 c, err := net.Dial("tcp", listener.Addr().String()) 238 assert.NoError(t, err) 239 240 fmt.Fprintf(c, "GET /example HTTP/1.0\r\n\r\n") 241 scanner := bufio.NewScanner(c) 242 var response string 243 for scanner.Scan() { 244 response += scanner.Text() 245 } 246 assert.Contains(t, response, "HTTP/1.0 200", "should get a 200") 247 assert.Contains(t, response, "it worked", "resp body should match") 248 } 249 250 func TestBadListener(t *testing.T) { 251 router := New() 252 addr, err := net.ResolveTCPAddr("tcp", "localhost:10086") 253 assert.NoError(t, err) 254 listener, err := net.ListenTCP("tcp", addr) 255 assert.NoError(t, err) 256 listener.Close() 257 assert.Error(t, router.RunListener(listener)) 258 } 259 260 func TestWithHttptestWithAutoSelectedPort(t *testing.T) { 261 router := New() 262 router.GET("/example", func(c *Context) { c.String(http.StatusOK, "it worked") }) 263 264 ts := httptest.NewServer(router) 265 defer ts.Close() 266 267 testRequest(t, ts.URL+"/example") 268 } 269 270 func TestConcurrentHandleContext(t *testing.T) { 271 router := New() 272 router.GET("/", func(c *Context) { 273 c.Request.URL.Path = "/example" 274 router.HandleContext(c) 275 }) 276 router.GET("/example", func(c *Context) { c.String(http.StatusOK, "it worked") }) 277 278 var wg sync.WaitGroup 279 iterations := 200 280 wg.Add(iterations) 281 for i := 0; i < iterations; i++ { 282 go func() { 283 testGetRequestHandler(t, router, "/") 284 wg.Done() 285 }() 286 } 287 wg.Wait() 288 } 289 290 // func TestWithHttptestWithSpecifiedPort(t *testing.T) { 291 // router := New() 292 // router.GET("/example", func(c *Context) { c.String(http.StatusOK, "it worked") }) 293 294 // l, _ := net.Listen("tcp", ":8033") 295 // ts := httptest.Server{ 296 // Listener: l, 297 // Config: &http.Server{Handler: router}, 298 // } 299 // ts.Start() 300 // defer ts.Close() 301 302 // testRequest(t, "http://localhost:8033/example") 303 // } 304 305 func testGetRequestHandler(t *testing.T, h http.Handler, url string) { 306 req, err := http.NewRequest(http.MethodGet, url, nil) 307 assert.NoError(t, err) 308 309 w := httptest.NewRecorder() 310 h.ServeHTTP(w, req) 311 312 assert.Equal(t, "it worked", w.Body.String(), "resp body should match") 313 assert.Equal(t, 200, w.Code, "should get a 200") 314 }