Skip to content

Instantly share code, notes, and snippets.

@swankjesse
Created July 10, 2018 02:26
Show Gist options
  • Save swankjesse/c04a0181da527ddb70b4dee49b804d13 to your computer and use it in GitHub Desktop.
Save swankjesse/c04a0181da527ddb70b4dee49b804d13 to your computer and use it in GitHub Desktop.
/*
* Copyright (C) 2018 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.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;
import java.util.concurrent.TimeUnit;
import okhttp3.mockwebserver.Dispatcher;
import okhttp3.mockwebserver.MockResponse;
import okhttp3.mockwebserver.MockWebServer;
import okhttp3.mockwebserver.RecordedRequest;
/**
* This server returns the current time on every request. The response is forged as 100 seconds old,
* and HTTP conditional caching rules suggest is should remain fresh for 10 seconds.
*
* Firefox 61 and Safari 11 both cache this response for 10 seconds. You can see this by opening the
* URL (http://localhost:8123) and clicking the link repeatedly. It should change after 10 seconds.
*
* Chrome does not cache the response. Every time you click the link you should see the current
* time.
*/
public class MustRevalidateServer {
public void run() throws IOException {
MockWebServer server = new MockWebServer();
server.setDispatcher(new Dispatcher() {
@Override public MockResponse dispatch(RecordedRequest request) {
return new MockResponse()
.addHeader("content-type: text/html; charset=utf-8")
.addHeader("cache-control", "private, must-revalidate")
.addHeader("last-modified: " + formatDate(-100, TimeUnit.SECONDS))
.addHeader("date: " + formatDate(0, TimeUnit.SECONDS))
.setBody("<a href=\"\">" + formatDate(0, TimeUnit.SECONDS) + "</a>");
}
});
server.start(8123);
}
private String formatDate(long delta, TimeUnit timeUnit) {
return formatDate(new Date(System.currentTimeMillis() + timeUnit.toMillis(delta)));
}
private String formatDate(Date date) {
DateFormat rfc1123 = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss zzz", Locale.US);
rfc1123.setTimeZone(TimeZone.getTimeZone("GMT"));
return rfc1123.format(date);
}
public static void main(String[] args) throws IOException {
new MustRevalidateServer().run();
}
}
@swankjesse
Copy link
Author

The 100 seconds / 10 seconds explanation is here:
https://publicobject.com/2015/03/26/how-do-http-caching-heuristics-work/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment