TestPingHeadFallback tests that the client falls back to GET if HEAD fails.
(t *testing.T)
| 82 | |
| 83 | // TestPingHeadFallback tests that the client falls back to GET if HEAD fails. |
| 84 | func TestPingHeadFallback(t *testing.T) { |
| 85 | const expectedPath = "/_ping" |
| 86 | expMethods := []string{http.MethodHead, http.MethodGet} |
| 87 | |
| 88 | tests := []struct { |
| 89 | status int |
| 90 | expected []string |
| 91 | }{ |
| 92 | { |
| 93 | status: http.StatusOK, |
| 94 | expected: []string{http.MethodHead}, |
| 95 | }, |
| 96 | { |
| 97 | status: http.StatusInternalServerError, |
| 98 | expected: []string{http.MethodHead, http.MethodGet}, |
| 99 | }, |
| 100 | { |
| 101 | status: http.StatusNotFound, |
| 102 | expected: []string{http.MethodHead, http.MethodGet}, |
| 103 | }, |
| 104 | { |
| 105 | status: http.StatusMethodNotAllowed, |
| 106 | expected: []string{http.MethodHead, http.MethodGet}, |
| 107 | }, |
| 108 | } |
| 109 | |
| 110 | for _, tc := range tests { |
| 111 | t.Run(http.StatusText(tc.status), func(t *testing.T) { |
| 112 | var reqs []string |
| 113 | client, err := New(WithBaseMockClient(func(req *http.Request) (*http.Response, error) { |
| 114 | if !strings.HasPrefix(req.URL.Path, expectedPath) { |
| 115 | return nil, fmt.Errorf("expected URL '%s', got '%s'", expectedPath, req.URL.Path) |
| 116 | } |
| 117 | if !slices.Contains(expMethods, req.Method) { |
| 118 | return nil, fmt.Errorf("expected one of '%v', got '%s'", expMethods, req.Method) |
| 119 | } |
| 120 | reqs = append(reqs, req.Method) |
| 121 | resp := &http.Response{StatusCode: http.StatusOK, Header: http.Header{}} |
| 122 | if req.Method == http.MethodHead { |
| 123 | resp.StatusCode = tc.status |
| 124 | } |
| 125 | resp.Header.Add("Api-Version", "1.2.3") |
| 126 | return resp, nil |
| 127 | })) |
| 128 | assert.NilError(t, err) |
| 129 | ping, _ := client.Ping(t.Context(), PingOptions{}) |
| 130 | assert.Check(t, is.Equal(ping.APIVersion, "1.2.3")) |
| 131 | assert.Check(t, is.DeepEqual(reqs, tc.expected)) |
| 132 | }) |
| 133 | } |
| 134 | } |
nothing calls this directly
no test coverage detected
searching dependent graphs…