JsonMediaTypeHandler implements MediaTypeHandler { @Override public String createXmlRepresentation(HttpResponse response) { try { if (response == null || response.getContentAsString() == null) { return null; } String content = response.getContentAsString().trim(); if (!StringUtils.hasContent(content)) { return null; } content = content.replaceAll("\\\\u0000", ""); JSON json = new JsonUtil().parseTrimmedText(content); JsonXmlSerializer serializer = new JsonXmlSerializer(); serializer.setTypeHintsEnabled(false); serializer.setRootName(HttpUtils.isErrorStatus(response.getStatusCode()) ? "Fault" : "Response"); URL url = response.getURL(); String originalUri = readOriginalUriFrom(response.getRequest()); String namespaceUri = originalUri != null ? originalUri : makeNamespaceUriFrom(url); serializer.setNamespace("", namespaceUri); content = serializer.write(json); content = XmlUtils.prettyPrintXml(content); return content; } catch (JSONException ignore) { } catch (Exception e) { SoapUI.logError(e); } return "<xml/>"; }  boolean canHandle(String contentType); @Override String createXmlRepresentation(HttpResponse response); String createXmlRepresentation(TypedContent typedContent); static String makeNamespaceUriFrom(URL url);  }
@Test public void retainsUriInFirstSubmitAsNamespaceUri() throws Exception { HttpResponse response = submitRequestAndReceiveResponse(restRequest, "/original/path"); String originalXml = mediaTypeHandler.createXmlRepresentation(response); HttpResponse responseWithNewPath = submitRequestAndReceiveResponse(restRequest, "/another/path"); assertThat(mediaTypeHandler.createXmlRepresentation(responseWithNewPath), is(equalTo(originalXml))); }
@Test public void usesActualUriWhenPathContainsTemplateParameters() throws Exception { RestParamProperty userParameter = restRequest.getParams().addProperty("user"); userParameter.setStyle(RestParamsPropertyHolder.ParameterStyle.TEMPLATE); userParameter.setValue("billy"); String originalPath = "/original/{user}"; restRequest.setPath(originalPath); SubmitContext submitContext = submitRequest(restRequest, originalPath); HttpResponse response = makeResponseFor(restRequest, "/original/billy"); restRequest.setResponse(response, submitContext); assertThat(mediaTypeHandler.createXmlRepresentation(response), containsString("/original/billy")); }