/** Tests jsonp with custom callback function */ @Test public void testCustomJSONP() throws Exception { String layer = MockData.FORESTS.getPrefix() + ":" + MockData.FORESTS.getLocalPart(); String request = "wms?version=1.1.1" + "&request=DescribeLayer" + "&layers=" + layer + "&query_layers=" + layer + "&width=20&height=20" + "&outputFormat=" + JSONType.jsonp + "&format_options=" + JSONType.CALLBACK_FUNCTION_KEY + ":DescribeLayer"; JSONType.setJsonpEnabled(true); String result = getAsString(request); JSONType.setJsonpEnabled(false); checkJSONPDescribeLayer(result, layer); }
@Test public void testDescribePrimitiveGeoFeatureJSONP() throws Exception { JSONType.setJsonpEnabled(true); String output = getAsString( "wfs?service=WFS&request=DescribeFeatureType&version=1.0.0&outputFormat=" + JSONType.jsonp + "&typeName=" + getLayerId(SystemTestData.PRIMITIVEGEOFEATURE)); JSONType.setJsonpEnabled(false); // removing specific parts output = output.substring(0, output.length() - 2); output = output.substring(JSONType.CALLBACK_FUNCTION.length() + 1, output.length()); testOutput(output); }
@Test public void testJsonException130() throws Exception { String path = "wms?version=1.3.0&request=getmap&layers=foobar&EXCEPTIONS=" + JSONType.jsonp + "&format_options=callback:myMethod"; JSONType.setJsonpEnabled(true); MockHttpServletResponse response = getAsServletResponse(path); JSONType.setJsonpEnabled(false); String content = response.getContentAsString(); testJson(testJsonP(content)); }
@Test public void testDescribePrimitiveGeoFeatureJSONPCustom() throws Exception { JSONType.setJsonpEnabled(true); String output = getAsString( "wfs?service=WFS&request=DescribeFeatureType&version=1.0.0&outputFormat=" + JSONType.jsonp + "&typeName=" + getLayerId(SystemTestData.PRIMITIVEGEOFEATURE) + "&format_options=" + JSONType.CALLBACK_FUNCTION_KEY + ":custom"); JSONType.setJsonpEnabled(false); // removing specific parts assertTrue(output.startsWith("custom(")); output = output.substring(0, output.length() - 2); output = output.substring("custom".length() + 1, output.length()); testOutput(output); } }
private void testJsonpException(String wfsVersion) throws UnsupportedEncodingException, Exception { String path = getPath(wfsVersion); JSONType.setJsonpEnabled(true); MockHttpServletResponse response = getAsServletResponse(path + "&EXCEPTIONS=" + JSONType.jsonp); JSONType.setJsonpEnabled(false); // MimeType assertEquals(JSONType.jsonp, response.getContentType()); // Content String content = response.getContentAsString(); testJson(testJsonP(content), wfsVersion); }
@Test public void testMimeType() { // MimeType assertNotSame(JSONType.json, JSONType.jsonp); assertTrue(JSONType.isJsonMimeType(JSONType.json)); // enable JsonP programmatically JSONType.setJsonpEnabled(true); // check jsonp is enabled assertTrue(JSONType.useJsonp(JSONType.jsonp)); // disable JsonP JSONType.setJsonpEnabled(false); assertFalse(JSONType.useJsonp(JSONType.jsonp)); }
@Test public void testCallbackFunction() throws Exception { JSONType.setJsonpEnabled(true); MockHttpServletResponse resp = getAsServletResponse( "wfs?request=GetFeature&version=1.0.0&typename=sf:PrimitiveGeoFeature&maxfeatures=1&outputformat=" + JSONType.jsonp + "&format_options=" + JSONType.CALLBACK_FUNCTION_KEY + ":myFunc"); JSONType.setJsonpEnabled(false); String out = resp.getContentAsString(); assertEquals(JSONType.jsonp, resp.getContentType()); assertTrue(out.startsWith("myFunc(")); assertTrue(out.endsWith(")")); // extract the json and check it out = out.substring(7, out.length() - 1); JSONObject rootObject = JSONObject.fromObject(out); assertEquals(rootObject.get("type"), "FeatureCollection"); JSONArray featureCol = rootObject.getJSONArray("features"); JSONObject aFeature = featureCol.getJSONObject(0); assertEquals(aFeature.getString("geometry_name"), "surfaceProperty"); }
JSONType.setJsonpEnabled(true); MockHttpServletResponse response = getAsServletResponse(request, ""); JSONType.setJsonpEnabled(false);
@Test public void testExceptions() throws Exception { Document doc = getAsDOM("wms?service=WMS&request=getCapabilities&version=1.1.1", true); XpathEngine xpath = XMLUnit.newXpathEngine(); assertTrue( xpath.evaluate("//Exception/Format[1]", doc).equals("application/vnd.ogc.se_xml")); assertTrue( xpath.evaluate("//Exception/Format[2]", doc) .equals("application/vnd.ogc.se_inimage")); assertTrue( xpath.evaluate("//Exception/Format[3]", doc) .equals("application/vnd.ogc.se_blank")); assertTrue(xpath.evaluate("//Exception/Format[4]", doc).equals("application/json")); assertTrue(xpath.getMatchingNodes("//Exception/Format", doc).getLength() >= 4); boolean jsonpOriginal = JSONType.isJsonpEnabled(); try { JSONType.setJsonpEnabled(true); doc = getAsDOM("wms?service=WMS&request=getCapabilities&version=1.1.1", true); assertTrue(xpath.evaluate("//Exception/Format[5]", doc).equals("text/javascript")); assertTrue(xpath.getMatchingNodes("//Exception/Format", doc).getLength() == 5); JSONType.setJsonpEnabled(false); doc = getAsDOM("wms?service=WMS&request=getCapabilities&version=1.1.1", true); assertTrue(xpath.getMatchingNodes("//Exception/Format", doc).getLength() == 4); } finally { JSONType.setJsonpEnabled(jsonpOriginal); } }
+ ":custom"; JSONType.setJsonpEnabled(true); MockHttpServletResponse response = getAsServletResponse(request, ""); JSONType.setJsonpEnabled(false);
JSONType.setJsonpEnabled(true); doc = getAsDOM("wms?service=WMS&request=getCapabilities&version=1.3.0", true); assertXpathEvaluatesTo( "count(wms:WMS_Capabilities/wms:Capability/wms:Exception/wms:Format)", doc); JSONType.setJsonpEnabled(false); doc = getAsDOM("wms?service=WMS&request=getCapabilities&version=1.3.0", true); assertXpathEvaluatesTo( doc); } finally { JSONType.setJsonpEnabled(jsonpOriginal);