BindToXMLPayload implements Binder { @Override public <R extends HttpRequest> R bindToRequest(final R request, final Object input) { try { String xml = xmlParser.toXML(checkNotNull(input, "input")); request.setPayload(xml); MutableContentMetadata metadata = request.getPayload().getContentMetadata(); if (contentTypeMustBeAdded(metadata)) { metadata.setContentType("application/xml"); } return request; } catch (IOException ex) { throw new BindException(request, ex); } } @Inject  BindToXMLPayload(final XMLParser xmlParser); @Override R bindToRequest(final R request, final Object input);  }
@Test public void testBindJAXBObject() throws SecurityException, NoSuchMethodException { BindToXMLPayload binder = new BindToXMLPayload(xml); TestJAXBDomain obj = new TestJAXBDomain(); obj.setElem("Hello World"); HttpRequest request = HttpRequest.builder().method("GET").endpoint("http: request = binder.bindToRequest(request, obj); assertEquals(request.getPayload().getRawContent(), XMLParser.DEFAULT_XML_HEADER + "\n<test>\n <elem>Hello World</elem>\n</test>\n"); assertEquals(request.getPayload().getContentMetadata().getContentType(), "application/xml"); }
@Test public void testHeaderIsChangedIfNeeded() throws SecurityException, NoSuchMethodException { BindToXMLPayload binder = new BindToXMLPayload(xml); TestJAXBDomain obj = new TestJAXBDomain(); obj.setElem("Hello World"); Multimap<String, String> headers = ImmutableMultimap.<String, String> of("Content-type", "application/unknown"); HttpRequest request = HttpRequest.builder().method("GET").endpoint("http: request = binder.bindToRequest(request, obj); assertEquals(request.getPayload().getRawContent(), XMLParser.DEFAULT_XML_HEADER + "\n<test>\n <elem>Hello World</elem>\n</test>\n"); assertEquals(request.getPayload().getContentMetadata().getContentType(), "application/xml"); }
@Test(expectedExceptions = NullPointerException.class) public void testNullIsBad() { BindToXMLPayload binder = new BindToXMLPayload(xml); binder.bindToRequest(HttpRequest.builder().method("GET").endpoint("http: }
@Test(expectedExceptions = BindException.class) public void testInvalidObjectBinding() { BindToXMLPayload binder = new BindToXMLPayload(xml); HttpRequest request = HttpRequest.builder().method("GET").endpoint("http: request = binder.bindToRequest(request, new Object()); }