(t *testing.T)
| 276 | } |
| 277 | |
| 278 | func TestCustomCookieName(t *testing.T) { |
| 279 | |
| 280 | customCookieName := "TestCustomCookieName" |
| 281 | |
| 282 | rt := NewRestTester(t, |
| 283 | &RestTesterConfig{ |
| 284 | DatabaseConfig: &DatabaseConfig{ |
| 285 | DbConfig: DbConfig{ |
| 286 | Name: "db", |
| 287 | SessionCookieName: customCookieName, |
| 288 | }, |
| 289 | }, |
| 290 | }, |
| 291 | ) |
| 292 | |
| 293 | defer rt.Close() |
| 294 | |
| 295 | // Disable guest user |
| 296 | a := auth.NewAuthenticator(rt.MetadataStore(), nil, rt.GetDatabase().AuthenticatorOptions(rt.Context())) |
| 297 | user, err := a.GetUser("") |
| 298 | assert.NoError(t, err) |
| 299 | user.SetDisabled(true) |
| 300 | err = a.Save(user) |
| 301 | assert.NoError(t, err) |
| 302 | |
| 303 | // Create a user |
| 304 | response := rt.SendAdminRequest("POST", "/db/_user/", `{"name":"user1", "password":"1234"}`) |
| 305 | RequireStatus(t, response, 201) |
| 306 | |
| 307 | // Create a session |
| 308 | resp := rt.SendRequest("POST", "/db/_session", `{"name":"user1", "password":"1234"}`) |
| 309 | assert.Equal(t, 200, resp.Code) |
| 310 | |
| 311 | // Extract the cookie from the create session response to verify the "Set-Cookie" value returned by Sync Gateway |
| 312 | result := resp.Result() |
| 313 | defer func() { assert.NoError(t, result.Body.Close()) }() |
| 314 | cookies := result.Cookies() |
| 315 | assert.True(t, len(cookies) == 1) |
| 316 | cookie := cookies[0] |
| 317 | assert.Equal(t, customCookieName, cookie.Name) |
| 318 | assert.Equal(t, "/db", cookie.Path) |
| 319 | |
| 320 | // Attempt to use default cookie name to authenticate -- expect a 401 error |
| 321 | headers := map[string]string{} |
| 322 | headers["Cookie"] = fmt.Sprintf("%s=%s", auth.DefaultCookieName, cookie.Value) |
| 323 | resp = rt.SendRequestWithHeaders("GET", "/{{.keyspace}}/foo", `{}`, headers) |
| 324 | RequireStatus(t, resp, http.StatusUnauthorized) |
| 325 | |
| 326 | // Attempt to use custom cookie name to authenticate |
| 327 | headers["Cookie"] = fmt.Sprintf("%s=%s", customCookieName, cookie.Value) |
| 328 | resp = rt.SendRequestWithHeaders("POST", "/{{.keyspace}}/", `{"_id": "foo", "key": "val"}`, headers) |
| 329 | RequireStatus(t, resp, http.StatusOK) |
| 330 | } |
| 331 | |
| 332 | // Test that TTL values greater than the default max offset TTL 2592000 seconds are processed correctly |
| 333 | // fixes #974 |
nothing calls this directly
no test coverage detected