Skip to content

Commit

Permalink
SOAP response body not set as text (#9)
Browse files Browse the repository at this point in the history
  • Loading branch information
Alex Rodriguez committed Jan 2, 2018
1 parent 2713038 commit a110d79
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 2 deletions.
7 changes: 5 additions & 2 deletions src/main/java/com/enonic/lib/http/client/ResponseMapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ public final class ResponseMapper
ImmutableSet.of( "okhttp-received-millis", "okhttp-selected-protocol", "okhttp-sent-millis" );

private final static ImmutableList<MediaType> TEXT_CONTENT_TYPES =
ImmutableList.of( MediaType.ANY_TEXT_TYPE, MediaType.create( "application", "xml" ), MediaType.create( "application", "json" ) );
ImmutableList.of( MediaType.ANY_TEXT_TYPE, MediaType.create( "application", "xml" ), MediaType.create( "application", "json" ),
MediaType.create( "application", "javascript" ), MediaType.create( "application", "soap+xml" ),
MediaType.create( "application", "xml" ) );

private final Response response;

Expand Down Expand Up @@ -136,7 +138,8 @@ private boolean isTextContent()
try
{
final MediaType mediaType = MediaType.parse( contentType );
return TEXT_CONTENT_TYPES.stream().anyMatch( mediaType::is );
final String subType = mediaType.subtype() == null ? "" : mediaType.subtype().toLowerCase();
return TEXT_CONTENT_TYPES.stream().anyMatch( mediaType::is ) || subType.contains( "xml" ) || subType.contains( "json" );
}
catch ( IllegalArgumentException e )
{
Expand Down
44 changes: 44 additions & 0 deletions src/test/java/com/enonic/lib/http/client/ResponseMapperTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,48 @@ public void serializeNoContentType()

JsonAssert.assertJson( getClass(), "response-no-type", mapper );
}

@Test
public void serializeSoapContentType()
throws Exception
{
final Request.Builder request = new Request.Builder();
request.url( "http://host/some/path" );
request.get();

final Response.Builder response = new Response.Builder();
final ResponseBody body = ResponseBody.create( MediaType.parse( "application/soap+xml; charset=utf-8" ),
"<?xml version=\"1.0\" encoding=\"utf-8\"?><body/>" );
response.body( body );
response.code( 200 );
response.message( "Ok" );
response.protocol( Protocol.HTTP_1_1 );
response.request( request.build() );
response.header( "Content-Type", "application/soap+xml; charset=utf-8" );
ResponseMapper mapper = new ResponseMapper( response.build() );

JsonAssert.assertJson( getClass(), "response-soap", mapper );
}

@Test
public void serializeXmlContentType()
throws Exception
{
final Request.Builder request = new Request.Builder();
request.url( "http://host/some/path" );
request.get();

final Response.Builder response = new Response.Builder();
final ResponseBody body = ResponseBody.create( MediaType.parse( "application/mathml+xml; charset=utf-8" ),
"<?xml version=\"1.0\" encoding=\"utf-8\"?><body/>" );
response.body( body );
response.code( 200 );
response.message( "Ok" );
response.protocol( Protocol.HTTP_1_1 );
response.request( request.build() );
response.header( "Content-Type", "application/mathml+xml; charset=utf-8" );
ResponseMapper mapper = new ResponseMapper( response.build() );

JsonAssert.assertJson( getClass(), "response-xml", mapper );
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"status": 200,
"message": "Ok",
"body": "<?xml version=\"1.0\" encoding=\"utf-8\"?><body/>",
"bodyStream": "ByteSource.wrap(3C3F786D6C2076657273696F6E3...)",
"contentType": "application/soap+xml; charset=utf-8",
"headers": {
"Content-Type": "application/soap+xml; charset=utf-8"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"status": 200,
"message": "Ok",
"body": "<?xml version=\"1.0\" encoding=\"utf-8\"?><body/>",
"bodyStream": "ByteSource.wrap(3C3F786D6C2076657273696F6E3...)",
"contentType": "application/mathml+xml; charset=utf-8",
"headers": {
"Content-Type": "application/mathml+xml; charset=utf-8"
}
}

0 comments on commit a110d79

Please sign in to comment.