mirror of
https://github.com/square/okhttp.git
synced 2025-11-24 18:41:06 +03:00
Change CookieJar to deal with Cookies, not Headers.
There's a big consequence to this for compatible with RFC 2965 Cookies. In particular, this drops our ability to support 'Set-Cookie2' headers, and some features used there including quoted attributes like 'Max-Age="25"'. This is the right move for interop with browsers, and likely to make things better for application developers, but some people who strictly followed the RFC 2965 spec will be broken. (That spec was never seriously adopted anywhere, which is the entire motivation of RFC 6265.) The upside is the CookieJar interface is now much more straightforward. This is particularly good going forward, and for clients who don't have to worry about the strangeness of things like $Version=1 in the RFC 2965 spec.
This commit is contained in:
@@ -0,0 +1,56 @@
|
||||
/*
|
||||
* Copyright (C) 2015 Square, Inc.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package okhttp3;
|
||||
|
||||
import java.util.ArrayDeque;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.Deque;
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
public final class RecordingCookieJar implements CookieJar {
|
||||
private final Deque<List<Cookie>> requestCookies = new ArrayDeque<>();
|
||||
private final Deque<List<Cookie>> responseCookies = new ArrayDeque<>();
|
||||
|
||||
public void enqueueRequestCookies(Cookie... cookies) {
|
||||
requestCookies.add(Arrays.asList(cookies));
|
||||
}
|
||||
|
||||
public List<Cookie> takeResponseCookies() {
|
||||
return responseCookies.removeFirst();
|
||||
}
|
||||
|
||||
public void assertResponseCookies(String... cookies) {
|
||||
List<Cookie> actualCookies = takeResponseCookies();
|
||||
List<String> actualCookieStrings = new ArrayList<>();
|
||||
for (Cookie cookie : actualCookies) {
|
||||
actualCookieStrings.add(cookie.toString());
|
||||
}
|
||||
assertEquals(Arrays.asList(cookies), actualCookieStrings);
|
||||
}
|
||||
|
||||
@Override public void saveFromResponse(HttpUrl url, List<Cookie> cookies) {
|
||||
responseCookies.add(cookies);
|
||||
}
|
||||
|
||||
@Override public List<Cookie> loadForRequest(HttpUrl url) {
|
||||
if (requestCookies.isEmpty()) return Collections.emptyList();
|
||||
return requestCookies.removeFirst();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user