BindUserMetadataToHeadersWithPrefix implements Binder { @Override public <R extends HttpRequest> R bindToRequest(R request, Object input) { checkArgument(checkNotNull(input, "input") instanceof Blob, "this binder is only valid for Blobs!"); checkNotNull(request, "request"); return metadataPrefixer.bindToRequest(request, Blob.class.cast(input).getMetadata().getUserMetadata()); } @Inject  BindUserMetadataToHeadersWithPrefix(BindMapToHeadersWithPrefix metadataPrefixer); @Override R bindToRequest(R request, Object input);  }
@Test public void testCorrect() throws SecurityException, NoSuchMethodException { HttpRequest request = HttpRequest.builder().method("GET").endpoint("http: BindUserMetadataToHeadersWithPrefix binder = new BindUserMetadataToHeadersWithPrefix( new BindMapToHeadersWithPrefix("prefix:")); Blob blob = ContextBuilder.newBuilder("transient").buildInjector().getInstance(Blob.Factory.class).create(null); blob.getMetadata().setUserMetadata(ImmutableMap.of("imageName", "foo", "serverId", "2")); assertEquals( binder.bindToRequest(request, blob), HttpRequest.builder().method("GET").endpoint("http: .addHeader("prefix:imagename", "foo") .addHeader("prefix:serverid", "2").build()); }
@Test(expectedExceptions = IllegalArgumentException.class) public void testMustBeBlob() { BindUserMetadataToHeadersWithPrefix binder = new BindUserMetadataToHeadersWithPrefix( new BindMapToHeadersWithPrefix("prefix:")); HttpRequest request = HttpRequest.builder().method("POST").endpoint("http: binder.bindToRequest(request, new File("foo")); }
@Test(expectedExceptions = { NullPointerException.class, IllegalStateException.class }) public void testNullIsBad() { BindUserMetadataToHeadersWithPrefix binder = new BindUserMetadataToHeadersWithPrefix( new BindMapToHeadersWithPrefix("prefix:")); HttpRequest request = HttpRequest.builder().method("GET").endpoint("http: binder.bindToRequest(request, null); }