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

Function TestUnixSocket

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

Source from the content-addressed store, hash-verified

246}
247
248func TestUnixSocket(t *testing.T) {
249 router := New()
250
251 unixTestSocket := filepath.Join(os.TempDir(), "unix_unit_test")
252
253 defer os.Remove(unixTestSocket)
254
255 go func() {
256 router.GET("/example", func(c *Context) { c.String(http.StatusOK, "it worked") })
257 assert.NoError(t, router.RunUnix(unixTestSocket))
258 }()
259 // have to wait for the goroutine to start and run the server
260 // otherwise the main thread will complete
261 time.Sleep(5 * time.Millisecond)
262
263 c, err := net.Dial("unix", unixTestSocket)
264 require.NoError(t, err)
265
266 fmt.Fprint(c, "GET /example HTTP/1.0\r\n\r\n")
267 scanner := bufio.NewScanner(c)
268 var responseBuilder strings.Builder
269 for scanner.Scan() {
270 responseBuilder.WriteString(scanner.Text())
271 }
272 response := responseBuilder.String()
273 assert.Contains(t, response, "HTTP/1.0 200", "should get a 200")
274 assert.Contains(t, response, "it worked", "resp body should match")
275}
276
277func TestBadUnixSocket(t *testing.T) {
278 router := New()

Callers

nothing calls this directly

Calls 5

NewFunction · 0.85
RunUnixMethod · 0.80
GETMethod · 0.65
StringMethod · 0.65
WriteStringMethod · 0.65

Tested by

no test coverage detected