MCPcopy
hub / github.com/gofiber/fiber / Test_App_OptionsAsterisk

Function Test_App_OptionsAsterisk

app_test.go:1626–1696  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

1624}
1625
1626func Test_App_OptionsAsterisk(t *testing.T) {
1627 t.Parallel()
1628
1629 app := New()
1630 app.Options("/resource", func(c Ctx) error {
1631 c.Set(HeaderAllow, "GET")
1632 c.Status(StatusNoContent)
1633
1634 return nil
1635 })
1636 app.Options("*", func(c Ctx) error {
1637 c.Set(HeaderAllow, "GET, POST")
1638 c.Status(StatusOK)
1639
1640 return nil
1641 })
1642
1643 ln := fasthttputil.NewInmemoryListener()
1644 errCh := make(chan error, 1)
1645 serverReady := make(chan struct{})
1646
1647 go func() {
1648 serverReady <- struct{}{}
1649 errCh <- app.Listener(ln)
1650 }()
1651
1652 <-serverReady
1653
1654 t.Cleanup(func() {
1655 require.NoError(t, app.Shutdown())
1656 require.NoError(t, <-errCh)
1657 })
1658
1659 writeRequest := func(conn net.Conn, raw string) {
1660 t.Helper()
1661 _, err := conn.Write([]byte(raw))
1662 require.NoError(t, err)
1663 }
1664
1665 conn, err := ln.Dial()
1666 require.NoError(t, err)
1667
1668 writeRequest(conn, "OPTIONS * HTTP/1.1\r\nHost: example.com\r\n\r\n")
1669
1670 resp, err := http.ReadResponse(bufio.NewReader(conn), &http.Request{Method: http.MethodOptions})
1671 require.NoError(t, err)
1672 require.Equal(t, StatusOK, resp.StatusCode)
1673 require.Equal(t, "GET, POST", resp.Header.Get(HeaderAllow))
1674 require.Zero(t, resp.ContentLength)
1675 body, err := io.ReadAll(resp.Body)
1676 require.NoError(t, err)
1677 require.Empty(t, body)
1678 require.NoError(t, resp.Body.Close())
1679 require.NoError(t, conn.Close())
1680
1681 controlConn, err := ln.Dial()
1682 require.NoError(t, err)
1683

Callers

nothing calls this directly

Calls 9

ListenerMethod · 0.80
ShutdownMethod · 0.80
NewFunction · 0.70
OptionsMethod · 0.65
SetMethod · 0.65
StatusMethod · 0.65
WriteMethod · 0.65
GetMethod · 0.65
CloseMethod · 0.65

Tested by

no test coverage detected