DefaultHeaderFactory implements HeaderFactory { @Override public void buildHeaders(RequestBuilder requestBuilder, RestAction<?> action, String securityToken) { List<HttpParameter> parameters = buildParameters(action, securityToken); addHeaders(requestBuilder, parameters); } @Inject  DefaultHeaderFactory(
            @GlobalHeaderParams RestParameterBindings globalParams,
            @XsrfHeaderName String securityHeaderName,
            @RestApplicationPath Provider<String> applicationPathProvider); @Override void buildHeaders(RequestBuilder requestBuilder, RestAction<?> action, String securityToken);  }
@Test public void build_securityToken_securityHeaderIsSet() { RestAction<Void> action = new SecuredRestAction(GET, RELATIVE_PATH); factory.buildHeaders(requestBuilder, action, SECURITY_TOKEN); verify(requestBuilder).setHeader(eq(XSRF_HEADER_NAME), eq(SECURITY_TOKEN)); }
@Test public void build_emptySecurityToken_securityHeaderIsNotSet() { RestAction<Void> action = new SecuredRestAction(GET, RELATIVE_PATH); factory.buildHeaders(requestBuilder, action, ""); verify(requestBuilder, never()).setHeader(eq(XSRF_HEADER_NAME), anyString()); }
@Test public void build_actionNotSecured_securityHeaderIsNotSet() { RestAction<Void> action = new UnsecuredRestAction(GET, RELATIVE_PATH); factory.buildHeaders(requestBuilder, action, SECURITY_TOKEN); verify(requestBuilder, never()).setHeader(eq(XSRF_HEADER_NAME), anyString()); }
@Test public void build_globalHeadersWithHttpMethod_areSetForSameHttpMethod() { RestAction<Void> action = new SecuredRestAction(GET, RELATIVE_PATH); factory.buildHeaders(requestBuilder, action, SECURITY_TOKEN); verify(requestBuilder).setHeader(eq(KEY_1), eq(VALUE_1)); verify(requestBuilder).setHeader(eq(KEY_2), eq(VALUE_2)); verify(requestBuilder, never()).setHeader(eq(KEY_3), eq(VALUE_3)); }
@Test public void build_actionHeaders_areSet() { RestAction<Void> action = createActionWithHeaderParams(); factory.buildHeaders(requestBuilder, action, SECURITY_TOKEN); verify(requestBuilder).setHeader(eq(ACTION_KEY_1), eq(VALUE_1)); verify(requestBuilder).setHeader(eq(ACTION_KEY_2), eq(VALUE_2)); }
@Test public void build_actionHeaderAreSetAfterGlobalHeaders() { RestAction<Void> action = createActionWithHeaderParams(); factory.buildHeaders(requestBuilder, action, SECURITY_TOKEN); InOrder inOrder = inOrder(requestBuilder); inOrder.verify(requestBuilder).setHeader(eq(KEY_1), anyString()); inOrder.verify(requestBuilder).setHeader(eq(KEY_2), anyString()); inOrder.verify(requestBuilder).setHeader(eq(ACTION_KEY_1), anyString()); inOrder.verify(requestBuilder).setHeader(eq(ACTION_KEY_2), anyString()); }
@Test public void build_acceptContentTypes_acceptHeaderIsSet() { UnsecuredRestAction action = new UnsecuredRestAction(GET, RELATIVE_PATH); action.setClientConsumedContentTypes(Arrays.asList(ContentType.valueOf("a"), ContentType.valueOf("b"))); factory.buildHeaders(requestBuilder, action, SECURITY_TOKEN); verify(requestBuilder).setHeader(eq(HttpHeaders.ACCEPT), eq("a/*,b/*")); }
@Test public void build_emptyRestApplicationPath_headerIsNotSet(@GlobalHeaderParams RestParameterBindings bindings) { factory = new DefaultHeaderFactory(bindings, XSRF_HEADER_NAME, () -> ""); UnsecuredRestAction action = new UnsecuredRestAction(GET, RELATIVE_PATH); factory.buildHeaders(requestBuilder, action, ""); verify(requestBuilder, never()).setHeader(eq(RpcRequestBuilder.MODULE_BASE_HEADER), anyString()); }
@Test public void build_notEmptyRestApplicationPath_headerIsNotSet(@GlobalHeaderParams RestParameterBindings bindings) { factory = new DefaultHeaderFactory(bindings, XSRF_HEADER_NAME, applicationPathProvider); UnsecuredRestAction action = new UnsecuredRestAction(GET, RELATIVE_PATH); factory.buildHeaders(requestBuilder, action, ""); verify(requestBuilder).setHeader(eq(RpcRequestBuilder.MODULE_BASE_HEADER), eq(APPLICATION_PATH)); }