MCPcopy
hub / github.com/gin-gonic/gin / TestFileDescriptor

Function TestFileDescriptor

gin_integration_test.go:298–337  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

296}
297
298func TestFileDescriptor(t *testing.T) {
299 router := New()
300
301 addr, err := net.ResolveTCPAddr("tcp", "localhost:0")
302 require.NoError(t, err)
303 listener, err := net.ListenTCP("tcp", addr)
304 require.NoError(t, err)
305 socketFile, err := listener.File()
306 if isWindows() {
307 // not supported by windows, it is unimplemented now
308 require.Error(t, err)
309 } else {
310 require.NoError(t, err)
311 }
312
313 if socketFile == nil {
314 return
315 }
316
317 go func() {
318 router.GET("/example", func(c *Context) { c.String(http.StatusOK, "it worked") })
319 assert.NoError(t, router.RunFd(int(socketFile.Fd())))
320 }()
321 // have to wait for the goroutine to start and run the server
322 // otherwise the main thread will complete
323 time.Sleep(5 * time.Millisecond)
324
325 c, err := net.Dial("tcp", listener.Addr().String())
326 require.NoError(t, err)
327
328 fmt.Fprintf(c, "GET /example HTTP/1.0\r\n\r\n")
329 scanner := bufio.NewScanner(c)
330 var responseBuilder strings.Builder
331 for scanner.Scan() {
332 responseBuilder.WriteString(scanner.Text())
333 }
334 response := responseBuilder.String()
335 assert.Contains(t, response, "HTTP/1.0 200", "should get a 200")
336 assert.Contains(t, response, "it worked", "resp body should match")
337}
338
339func TestBadFileDescriptor(t *testing.T) {
340 router := New()

Callers

nothing calls this directly

Calls 8

NewFunction · 0.85
isWindowsFunction · 0.85
FileMethod · 0.80
RunFdMethod · 0.80
GETMethod · 0.65
StringMethod · 0.65
WriteStringMethod · 0.65
ErrorMethod · 0.45

Tested by

no test coverage detected