/** * Adds an {@link LazyHeaderFactory} that will be used to construct a value for the given * key lazily on a background thread. * * <p> Headers may have multiple values whose order is defined by the order in which * this method is called. </p> * * <p> This class does not prevent you from adding the same value to a given key multiple * times </p> */ public Builder addHeader(@NonNull String key, @NonNull LazyHeaderFactory factory) { if (isUserAgentDefault && USER_AGENT_HEADER.equalsIgnoreCase(key)) { return setHeader(key, factory); } copyIfNecessary(); getFactories(key).add(factory); return this; }
/** * Replaces all existing {@link LazyHeaderFactory LazyHeaderFactorys} for the given key * with the given {@link LazyHeaderFactory}. * * <p> If the given value is {@code null}, the header at the given key will be removed. </p> * * <p> Use {@link #setHeader(String, LazyHeaderFactory)} if obtaining the value requires I/O * (i.e. an OAuth token). </p> */ @SuppressWarnings({"UnusedReturnValue", "WeakerAccess"}) // Public API public Builder setHeader(@NonNull String key, @Nullable String value) { return setHeader(key, value == null ? null : new StringHeaderFactory(value)); }
@Test public void testUpdatingBuilderAfterBuildingDoesNotModifyOriginalHeaders() { Builder builder = new Builder(); builder.addHeader("key", "firstValue"); builder.addHeader("otherKey", "otherValue"); LazyHeaders first = builder.build(); LazyHeaderFactory factory = mock(LazyHeaderFactory.class); when(factory.buildHeader()).thenReturn("otherValue"); builder.addHeader("key", "secondValue"); builder.setHeader("otherKey", factory); LazyHeaders second = builder.build(); assertThat(first.getHeaders()).isNotEqualTo(second.getHeaders()); assertThat(first.getHeaders()).containsEntry("key", "firstValue"); assertThat(first.getHeaders()).containsEntry("otherKey", "otherValue"); assertThat(second.getHeaders()).containsEntry("key", "firstValue,secondValue"); assertThat(second.getHeaders()).containsEntry("otherKey", "otherValue"); }
/** * Adds an {@link LazyHeaderFactory} that will be used to construct a value for the given * key lazily on a background thread. * * <p> Headers may have multiple values whose order is defined by the order in which * this method is called. </p> * * <p> This class does not prevent you from adding the same value to a given key multiple * times </p> */ public Builder addHeader(String key, LazyHeaderFactory factory) { if (isUserAgentDefault && USER_AGENT_HEADER.equalsIgnoreCase(key)) { return setHeader(key, factory); } copyIfNecessary(); getFactories(key).add(factory); return this; }
/** * Replaces all existing {@link LazyHeaderFactory LazyHeaderFactorys} for the given key * with the given {@link LazyHeaderFactory}. * * <p> If the given value is {@code null}, the header at the given key will be removed. </p> * * <p> Use {@link #setHeader(String, LazyHeaderFactory)} if obtaining the value requires I/O * (i.e. an OAuth token). </p> */ public Builder setHeader(String key, String value) { return setHeader(key, value == null ? null : new StringHeaderFactory(value)); }
/** * Adds an {@link LazyHeaderFactory} that will be used to construct a value for the given * key lazily on a background thread. * * <p> Headers may have multiple values whose order is defined by the order in which * this method is called. </p> * * <p> This class does not prevent you from adding the same value to a given key multiple * times </p> */ public Builder addHeader(String key, LazyHeaderFactory factory) { if ((isEncodingDefault && ENCODING_HEADER.equalsIgnoreCase(key)) || (isUserAgentDefault && USER_AGENT_HEADER.equalsIgnoreCase(key))) { return setHeader(key, factory); } copyIfNecessary(); getFactories(key).add(factory); return this; }
/** * Replaces all existing {@link LazyHeaderFactory LazyHeaderFactorys} for the given key * with the given {@link LazyHeaderFactory}. * * <p> If the given value is {@code null}, the header at the given key will be removed. </p> * * <p> Use {@link #setHeader(String, LazyHeaderFactory)} if obtaining the value requires I/O * (ie an oauth token). </p> */ public Builder setHeader(String key, String value) { return setHeader(key, value == null ? null : new StringHeaderFactory(value)); }
@Test public void testRemovingAndAddingEncodingHeaderReplacesDefaultThenAppends() { Builder builder = new Builder(); builder.setHeader("Accept-Encoding", (String) null); LazyHeaders headers = builder.build(); assertThat(headers.getHeaders()).doesNotContainKey("Accept-Encoding"); builder.addHeader("Accept-Encoding", "false"); headers = builder.build(); assertThat(headers.getHeaders()).containsEntry("Accept-Encoding", "false"); builder.addHeader("Accept-Encoding", "true"); headers = builder.build(); assertThat(headers.getHeaders()).containsEntry("Accept-Encoding", "false,true"); }
@Test public void testSetHeaderWithNullStringRemovesExistingHeader() { Builder builder = new Builder(); builder.addHeader("key", "first") .addHeader("key", "second") .setHeader("key", (String) null); LazyHeaders headers = builder.build(); assertThat(headers.getHeaders()).doesNotContainKey("key"); }
@Test public void testSetHeaderReplacesExistingHeaders() { Builder builder = new Builder(); builder.addHeader("key", "first") .addHeader("key", "second") .setHeader("key", "third"); LazyHeaders headers = builder.build(); assertThat(headers.getHeaders()).containsEntry("key", "third"); }
@Test public void testKeyNotIncludedWithFactoryThatReturnsNullValue() { Builder builder = new Builder(); builder.setHeader("test", new LazyHeaderFactory() { @Nullable @Override public String buildHeader() { return null; } }); LazyHeaders headers = builder.build(); assertThat(headers.getHeaders()).doesNotContainKey("test"); }
@Test public void testKeyNotIncludedWithFactoryThatReturnsEmptyValue() { Builder builder = new Builder(); builder.setHeader("test", new LazyHeaderFactory() { @Nullable @Override public String buildHeader() { return ""; } }); LazyHeaders headers = builder.build(); assertThat(headers.getHeaders()).doesNotContainKey("test"); }
@Test public void testRemovingAndAddingUserAgentHeaderReplacesDefaultThenAppends() { Builder builder = new Builder(); builder.setHeader("User-Agent", (String) null); LazyHeaders headers = builder.build(); assertThat(headers.getHeaders()).doesNotContainKey("User-Agent"); builder.addHeader("User-Agent", "false"); headers = builder.build(); assertThat(headers.getHeaders()).containsEntry("User-Agent", "false"); builder.addHeader("User-Agent", "true"); headers = builder.build(); assertThat(headers.getHeaders()).containsEntry("User-Agent", "false,true"); }
@Test public void testSetHeaderWithNullLazyHeaderFactoryRemovesExistingHeader() { Builder builder = new Builder(); builder.addHeader("key", "first") .addHeader("key", "second") .setHeader("key", (LazyHeaderFactory) null); LazyHeaders headers = builder.build(); assertThat(headers.getHeaders()).doesNotContainKey("key"); }