public Builder() { this.headers = new HttpHeaders.Builder(); this.body = new byte[0]; this.cookies = new ArrayList<>(); }
LiveHttpRequest(Builder builder) { this.id = builder.id == null ? randomUUID() : builder.id; this.version = builder.version; this.method = builder.method; this.url = builder.url; this.headers = builder.headers.build(); this.body = requireNonNull(builder.body); }
/** * Sets the (only) value for the header with the specified name. * <p/> * All existing values for the same header will be removed. * * @param name The name of the header * @param value The value of the header * @return {@code this} */ public SELF header(CharSequence name, Object value) { checkNotCookie(name); headers.set(name, checkNotNull(value)); return (SELF) this; }
/** * Adds cookies into the "Cookie" header. If the name matches an already existing cookie, the value will be overwritten. * <p> * Note that this requires decoding the current header value before re-encoding, so it is most efficient to * add all new cookies in one call to the method rather than spreading them out. * * @param cookies new cookies * @return this builder */ @Override public Builder addCookies(Collection<RequestCookie> cookies) { requireNonNull(cookies); Set<RequestCookie> currentCookies = decode(headers.get(COOKIE)); List<RequestCookie> newCookies = concat(cookies.stream(), currentCookies.stream()).collect(toList()); return cookies(newCookies); }
@Test public void transformsHeaders() { LiveHttpRequest request = LiveHttpRequest.get("/").headers( new HttpHeaders.Builder() .add("x", "y") .build()) .build() .newBuilder() .headers( new HttpHeaders.Builder() .add("a", "b") .build()) .build(); assertThat(request.header("x"), is(Optional.empty())); assertThat(request.header("a"), is(Optional.of("b"))); }
/** * Adds cookies into the "Cookie" header. If the name matches an already existing cookie, the value will be overwritten. * <p> * Note that this requires decoding the current header value before re-encoding, so it is most efficient to * add all new cookies in one call to the method rather than spreading them out. * * @param cookies new cookies * @return {@code this} */ public Builder addCookies(Collection<RequestCookie> cookies) { requireNonNull(cookies); Set<RequestCookie> currentCookies = decode(headers.get(COOKIE)); List<RequestCookie> newCookies = concat(cookies.stream(), currentCookies.stream()).collect(toList()); return cookies(newCookies); }
/** * Sets the cookies on this request by overwriting the value of the "Cookie" header. * * @param cookies cookies * @return this builder */ @Override public Builder cookies(Collection<RequestCookie> cookies) { requireNonNull(cookies); headers.remove(COOKIE); if (!cookies.isEmpty()) { header(COOKIE, encode(cookies)); } return this; }
/** * Sets the (only) value for the header with the specified name. * <p/> * All existing values for the same header will be removed. * * @param name The name of the header * @param value The value of the header * @return {@code this} */ public Builder header(CharSequence name, Object value) { checkNotCookie(name); this.headers.set(name, value); return this; }
/** * Sets the (only) value for the header with the specified name. * <p/> * All existing values for the same header will be removed. * * @param name The name of the header * @param values The value of the header * @return this builder */ public Builder set(CharSequence name, Iterable values) { nonNullValues(values) .ifPresent(nonNullValues -> this.nettyHeaders.set(name, nonNullValues)); return this; }
/** * Adds a new header with the specified {@code name} and {@code values}. * <p/> * Will not replace any existing values for the header. * * @param name The name of the header * @param values The value of the header * @return this builder */ public Builder add(CharSequence name, Iterable values) { nonNullValues(values) .ifPresent(nonNullValues -> this.nettyHeaders.add(name, nonNullValues)); return this; }
/** * Adds a new header with the specified {@code name} and {@code value}. * <p/> * Will not replace any existing values for the header. * * @param name The name of the header * @param value The value of the header * @return {@code this} */ public Builder addHeader(CharSequence name, Object value) { checkNotCookie(name); this.headers.add(name, value); return this; }
/** * Adds a new header with the specified {@code name} and {@code value}. * <p/> * Will not replace any existing values for the header. * * @param name The name of the header * @param value The value of the header * @return {@code this} */ public Builder addHeader(CharSequence name, Object value) { checkNotCookie(name); this.headers.add(name, value); return this; }
/** * Sets the (only) value for the header with the specified name. * <p/> * All existing values for the same header will be removed. * * @param name The name of the header * @param value The value of the header * @return {@code this} */ @Override public Builder header(CharSequence name, Object value) { this.headers.set(name, value); return this; }
/** * Adds a new header with the specified {@code name} and {@code value}. * <p/> * Will not replace any existing values for the header. * * @param name The name of the header * @param value The value of the header * @return {@code this} */ public SELF addHeader(CharSequence name, Object value) { checkNotCookie(name); headers.add(name, value); return (SELF) this; }
/** * Sets the (only) value for the header with the specified name. * <p/> * All existing values for the same header will be removed. * * @param name The name of the header * @param value The value of the header * @return {@code this} */ public SELF header(CharSequence name, String value) { checkNotCookie(name); headers.set(name, checkNotNull(value)); return (SELF) this; }
/** * Sets the cookies on this request by overwriting the value of the "Cookie" header. * * @param cookies cookies * @return {@code this} */ public Builder cookies(Collection<RequestCookie> cookies) { requireNonNull(cookies); headers.remove(COOKIE); if (!cookies.isEmpty()) { header(COOKIE, encode(cookies)); } return this; }
/** * Adds a new header with the specified {@code name} and {@code values}. * <p/> * Will not replace any existing values for the header. * * @param name The name of the header * @param values The value of the header * @return this builder */ public Builder add(CharSequence name, Iterable values) { nonNullValues(values) .ifPresent(nonNullValues -> this.nettyHeaders.add(name, nonNullValues)); return this; }
/** * Adds a new header with the specified {@code name} and {@code value}. * <p/> * Will not replace any existing values for the header. * * @param name The name of the header * @param value The value of the header * @return {@code this} */ public SELF addHeader(CharSequence name, String value) { checkNotCookie(name); headers.add(name, value); return (SELF) this; }
/** * Sets the (only) value for the header with the specified name. * <p/> * All existing values for the same header will be removed. * * @param name The name of the header * @param values The value of the header * @return {@code this} */ public SELF header(CharSequence name, Iterable<?> values) { checkNotCookie(name); headers.set(name, values); return (SELF) this; }
/** * Sets the (only) value for the header with the specified name. * <p/> * All existing values for the same header will be removed. * * @param name The name of the header * @param value The value of the header * @return {@code this} */ public Builder header(CharSequence name, Object value) { checkNotCookie(name); this.headers.set(name, value); return this; }