TextDelegateEncoder implements Encoder { @Override public void encode(Object object, Type bodyType, RequestTemplate template) throws EncodeException { Collection<String> contentTypes = HeaderAccessUtils.caseInsensitiveGet(template.headers(), HttpHeaders.CONTENT_TYPE); if (contentTypes == null) { contentTypes = ImmutableSet.of(); } if (contentTypes.size() == 1 && Iterables.getOnlyElement(contentTypes, "").equals(MediaType.TEXT_PLAIN)) { defaultEncoder.encode(object, bodyType, template); } else { delegate.encode(object, bodyType, template); } } TextDelegateEncoder(Encoder delegate); @Override void encode(Object object, Type bodyType, RequestTemplate template);  }
@Test public void usesDelegateWhenHeaderIsAbsent() { requestTemplate.headers(headers); textDelegateEncoder.encode(null, null, requestTemplate); verify(delegate).encode(null, null, requestTemplate); }
@Test public void usesDelegateWhenContentTypeIsNotTextPlain() { headers.put(HttpHeaders.CONTENT_TYPE, Arrays.asList(MediaType.APPLICATION_JSON)); requestTemplate.headers(headers); textDelegateEncoder.encode(null, null, requestTemplate); verify(delegate).encode(null, null, requestTemplate); }
@Test public void doesNotUseDelegateWhenContentTypeIsTextPlain() { headers.put(HttpHeaders.CONTENT_TYPE, Arrays.asList(MediaType.TEXT_PLAIN)); requestTemplate.headers(headers); textDelegateEncoder.encode(null, null, requestTemplate); verifyNoMoreInteractions(delegate); }
@Test public void doesNotUseDelegateWhenContentTypeHeaderHasArbitraryCase() { headers.put("Content-TYPE", Arrays.asList(MediaType.TEXT_PLAIN)); requestTemplate.headers(headers); textDelegateEncoder.encode(null, null, requestTemplate); verifyNoMoreInteractions(delegate); }