Skip to content

Commit

Permalink
Merge pull request #4479 from square/jwilson.1228.javadottime
Browse files Browse the repository at this point in the history
Use java.time in Headers
  • Loading branch information
swankjesse authored Dec 30, 2018
2 parents e3c9e9a + 3b9b8ff commit 17239e6
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 4 deletions.
48 changes: 46 additions & 2 deletions okhttp-tests/src/test/java/okhttp3/HeadersTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package okhttp3;

import java.io.IOException;
import java.time.Instant;
import java.util.Arrays;
import java.util.Collections;
import java.util.Date;
Expand Down Expand Up @@ -837,11 +838,12 @@ public final class HeadersTest {
}

@Test public void addDate() {
Date expected = new Date(0);
Date expected = new Date(0L);
Headers headers = new Headers.Builder()
.add("testDate", expected)
.build();
assertEquals("Thu, 01 Jan 1970 00:00:00 GMT", headers.get("testDate"));
assertEquals(new Date(0L), headers.getDate("testDate"));
}

@Test public void addDateNull() {
Expand All @@ -855,13 +857,34 @@ public final class HeadersTest {
}
}

@Test public void addInstant() {
Instant expected = Instant.ofEpochMilli(0L);
Headers headers = new Headers.Builder()
.add("Test-Instant", expected)
.build();
assertEquals("Thu, 01 Jan 1970 00:00:00 GMT", headers.get("Test-Instant"));
assertEquals(expected, headers.getInstant("Test-Instant"));
}

@Test public void addInstantNull() {
try {
new Headers.Builder()
.add("Test-Instant", (Instant) null)
.build();
fail();
} catch (NullPointerException expected) {
assertEquals("value for name Test-Instant == null", expected.getMessage());
}
}

@Test public void setDate() {
Date expected = new Date(1000);
Headers headers = new Headers.Builder()
.add("testDate", new Date(0))
.add("testDate", new Date(0L))
.set("testDate", expected)
.build();
assertEquals("Thu, 01 Jan 1970 00:00:01 GMT", headers.get("testDate"));
assertEquals(expected, headers.getDate("testDate"));
}

@Test public void setDateNull() {
Expand All @@ -874,4 +897,25 @@ public final class HeadersTest {
assertEquals("value for name testDate == null", expected.getMessage());
}
}

@Test public void setInstant() {
Instant expected = Instant.ofEpochMilli(1000L);
Headers headers = new Headers.Builder()
.add("Test-Instant", Instant.ofEpochMilli(0L))
.set("Test-Instant", expected)
.build();
assertEquals("Thu, 01 Jan 1970 00:00:01 GMT", headers.get("Test-Instant"));
assertEquals(expected, headers.getInstant("Test-Instant"));
}

@Test public void setInstantNull() {
try {
new Headers.Builder()
.set("Test-Instant", (Instant) null)
.build();
fail();
} catch (NullPointerException expected) {
assertEquals("value for name Test-Instant == null", expected.getMessage());
}
}
}
36 changes: 34 additions & 2 deletions okhttp/src/main/java/okhttp3/Headers.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

package okhttp3;

import java.time.Instant;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
Expand All @@ -30,6 +31,7 @@
import javax.annotation.Nullable;
import okhttp3.internal.Util;
import okhttp3.internal.http.HttpDate;
import org.codehaus.mojo.animal_sniffer.IgnoreJRERequirement;

/**
* The header fields of a single HTTP message. Values are uninterpreted strings; use {@code Request}
Expand Down Expand Up @@ -72,6 +74,16 @@ private Headers(String[] namesAndValues) {
return value != null ? HttpDate.parse(value) : null;
}

/**
* Returns the last value corresponding to the specified field parsed as an HTTP date, or null if
* either the field is absent or cannot be parsed as a date.
*/
@IgnoreJRERequirement
public @Nullable Instant getInstant(String name) {
Date value = getDate(name);
return value != null ? value.toInstant() : null;
}

/** Returns the number of field values. */
public int size() {
return namesAndValues.length / 2;
Expand Down Expand Up @@ -334,15 +346,25 @@ public Builder addAll(Headers headers) {
}

/**
* Add a header with the specified name and formatted Date.
* Does validation of header names and values.
* Add a header with the specified name and formatted date. Does validation of header names and
* value.
*/
public Builder add(String name, Date value) {
if (value == null) throw new NullPointerException("value for name " + name + " == null");
add(name, HttpDate.format(value));
return this;
}

/**
* Add a header with the specified name and formatted instant. Does validation of header names
* and value.
*/
@IgnoreJRERequirement
public Builder add(String name, Instant value) {
if (value == null) throw new NullPointerException("value for name " + name + " == null");
return add(name, new Date(value.toEpochMilli()));
}

/**
* Set a field with the specified date. If the field is not found, it is added. If the field is
* found, the existing values are replaced.
Expand All @@ -353,6 +375,16 @@ public Builder set(String name, Date value) {
return this;
}

/**
* Set a field with the specified instant. If the field is not found, it is added. If the field
* is found, the existing values are replaced.
*/
@IgnoreJRERequirement
public Builder set(String name, Instant value) {
if (value == null) throw new NullPointerException("value for name " + name + " == null");
return set(name, new Date(value.toEpochMilli()));
}

/**
* Add a field with the specified value without any validation. Only appropriate for headers
* from the remote peer or cache.
Expand Down

0 comments on commit 17239e6

Please sign in to comment.