@Test public void shouldSetHeaderOnResponse() { AddHeaderBean header = new AddHeaderBean(); header.setHeaderName("X-Clacks-Overhead"); header.setHeaderValue("GNU Terry Pratchett"); header.setOverwrite(false); header.setApplyTo(ApplyTo.RESPONSE); header.setValueType(ValueType.STRING); config.getAddHeaders().add(header); policy.apply(response, mContext, config, mResponseChain); assertEquals("GNU Terry Pratchett", response.getHeaders().get("X-Clacks-Overhead")); assertEquals(1, response.getHeaders().size()); }
@Test public void shouldNotOverwriteWhenFlagUnset() { request.getHeaders().put("X-Clacks-Overhead", "Ridcully"); AddHeaderBean header = new AddHeaderBean(); header.setHeaderName("X-Clacks-Overhead"); header.setHeaderValue("GNU Terry Pratchett"); header.setOverwrite(false); header.setApplyTo(ApplyTo.REQUEST); header.setValueType(ValueType.STRING); config.getAddHeaders().add(header); policy.apply(request, mContext, config, mRequestChain); assertEquals("Ridcully", request.getHeaders().get("X-Clacks-Overhead")); assertEquals(1, request.getHeaders().size()); }
@Test public void shouldGetValueFromSystemProperties() { System.setProperty("PROPERTIES_KEY", "42"); AddHeaderBean header = spy(new AddHeaderBean()); header.setHeaderName("the-meaning-of-life"); header.setHeaderValue("PROPERTIES_KEY"); header.setOverwrite(false); header.setApplyTo(ApplyTo.REQUEST); header.setValueType(ValueType.SYS); config.getAddHeaders().add(header); policy.apply(request, mContext, config, mRequestChain); assertEquals("42", request.getHeaders().get("the-meaning-of-life")); assertEquals(1, request.getHeaders().size()); }
@Test public void shouldGetValueFromEnvironment() { AddHeaderBean header = spy(new AddHeaderBean()); header.setHeaderName("the-meaning-of-life"); header.setHeaderValue("KEY_TO_THE_ENVIRONMENT"); header.setOverwrite(false); header.setApplyTo(ApplyTo.REQUEST); header.setValueType(ValueType.ENV); config.getAddHeaders().add(header); given(header.getResolvedHeaderValue()).willReturn("42"); policy.apply(request, mContext, config, mRequestChain); assertEquals("42", request.getHeaders().get("the-meaning-of-life")); assertEquals(1, request.getHeaders().size()); }
@Test public void shouldSetHeaderOnBoth() { AddHeaderBean header = new AddHeaderBean(); header.setHeaderName("Request-And-Response"); header.setHeaderValue("Weatherwax"); header.setOverwrite(false); header.setApplyTo(ApplyTo.BOTH); header.setValueType(ValueType.STRING); config.getAddHeaders().add(header); policy.apply(request, mContext, config, mRequestChain); assertEquals("Weatherwax", request.getHeaders().get("Request-And-Response")); assertEquals(1, request.getHeaders().size()); policy.apply(response, mContext, config, mResponseChain); assertEquals("Weatherwax", response.getHeaders().get("Request-And-Response")); assertEquals(1, response.getHeaders().size()); }