@Test(expectedExceptions = IllegalArgumentException.class) public void testMustBeMap() { BindMapToHeadersWithPrefix binder = new BindMapToHeadersWithPrefix("prefix:"); HttpRequest request = HttpRequest.builder().method("POST").endpoint("http://localhost").build(); ; binder.bindToRequest(request, new File("foo")); }
@Test public void testCorrect() throws SecurityException, NoSuchMethodException { HttpRequest request = HttpRequest.builder().method("GET").endpoint("http://momma").build(); BindMapToHeadersWithPrefix binder = new BindMapToHeadersWithPrefix("prefix:"); assertEquals( binder.bindToRequest(request, ImmutableMap.of("imageName", "foo", "serverId", "2")), HttpRequest.builder() .method("GET") .endpoint("http://momma") .addHeader("prefix:imagename", "foo") .addHeader("prefix:serverid", "2") .build()); }
@SuppressWarnings("unchecked") @Override public <R extends HttpRequest> R bindToRequest(R request, Object input) { checkArgument( checkNotNull(input, "input") instanceof ObjectMetadata, "this binder is only valid for ObjectMetadata!"); checkNotNull(request, "request"); ObjectMetadata md = ObjectMetadata.class.cast(input); checkArgument(md.getKey() != null, "objectMetadata.getKey() must be set!"); request = metadataPrefixer.bindToRequest(request, md.getUserMetadata()); Builder<String, String> headers = ImmutableMultimap.builder(); if (md.getCacheControl() != null) { headers.put(HttpHeaders.CACHE_CONTROL, md.getCacheControl()); } if (md.getContentMetadata().getContentDisposition() != null) { headers.put("Content-Disposition", md.getContentMetadata().getContentDisposition()); } if (md.getContentMetadata().getContentEncoding() != null) { headers.put("Content-Encoding", md.getContentMetadata().getContentEncoding()); } String contentLanguage = md.getContentMetadata().getContentLanguage(); if (contentLanguage != null) { headers.put(HttpHeaders.CONTENT_LANGUAGE, contentLanguage); } if (md.getContentMetadata().getContentType() != null) { headers.put(HttpHeaders.CONTENT_TYPE, md.getContentMetadata().getContentType()); } else { headers.put(HttpHeaders.CONTENT_TYPE, "binary/octet-stream"); } if (md.getContentMetadata().getContentMD5() != null) { headers.put("Content-MD5", base64().encode(md.getContentMetadata().getContentMD5())); } return (R) request.toBuilder().replaceHeaders(headers.build()).build(); }
@Test(expectedExceptions = {NullPointerException.class, IllegalStateException.class}) public void testNullIsBad() { BindMapToHeadersWithPrefix binder = new BindMapToHeadersWithPrefix("prefix:"); HttpRequest request = HttpRequest.builder().method("GET").endpoint("http://momma").build(); binder.bindToRequest(request, null); }