(t *testing.T)
| 136 | } |
| 137 | |
| 138 | func TestSignOut(t *testing.T) { |
| 139 | ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 140 | if r.URL.String() == "/api/v3/signout" && r.Method == "POST" { |
| 141 | w.WriteHeader(http.StatusNoContent) |
| 142 | } |
| 143 | })) |
| 144 | defer ts.Close() |
| 145 | |
| 146 | commonTs := startCommonTestServer() |
| 147 | defer commonTs.Close() |
| 148 | |
| 149 | correctEndpoint := fmt.Sprintf("%s/api", ts.URL) |
| 150 | testClient := NewRateLimitedHTTPClient() |
| 151 | |
| 152 | t.Run("success", func(t *testing.T) { |
| 153 | err := Signout(context.DnoteCtx{SessionKey: "somekey", APIEndpoint: correctEndpoint, HTTPClient: testClient}, "alice@example.com") |
| 154 | if err != nil { |
| 155 | t.Errorf("got signout request error: %+v", err.Error()) |
| 156 | } |
| 157 | }) |
| 158 | |
| 159 | t.Run("server error", func(t *testing.T) { |
| 160 | endpoint := fmt.Sprintf("%s/bad-api", commonTs.URL) |
| 161 | err := Signout(context.DnoteCtx{SessionKey: "somekey", APIEndpoint: endpoint, HTTPClient: testClient}, "alice@example.com") |
| 162 | if err == nil { |
| 163 | t.Error("error should have been returned") |
| 164 | } |
| 165 | }) |
| 166 | |
| 167 | t.Run("accidentally pointing to a catch-all handler", func(t *testing.T) { |
| 168 | endpoint := fmt.Sprintf("%s", commonTs.URL) |
| 169 | err := Signout(context.DnoteCtx{SessionKey: "somekey", APIEndpoint: endpoint, HTTPClient: testClient}, "alice@example.com") |
| 170 | |
| 171 | assert.Equal(t, errors.Cause(err), ErrContentTypeMismatch, "error cause mismatch") |
| 172 | }) |
| 173 | |
| 174 | // Gracefully handle a case where http client was not initialized in the context. |
| 175 | t.Run("nil HTTPClient", func(t *testing.T) { |
| 176 | err := Signout(context.DnoteCtx{SessionKey: "somekey", APIEndpoint: correctEndpoint}, "alice@example.com") |
| 177 | if err != nil { |
| 178 | t.Errorf("got signout request error: %+v", err.Error()) |
| 179 | } |
| 180 | }) |
| 181 | } |
| 182 | |
| 183 | func TestRateLimitedTransport(t *testing.T) { |
| 184 | var requestCount atomic.Int32 |
nothing calls this directly
no test coverage detected