package nl.hsac.fitnesse.fixture.slim; import org.apache.http.entity.ContentType; import org.junit.Test; import static junit.framework.TestCase.assertTrue; import static org.junit.Assert.assertEquals; import static org.junit.Assert.fail; /** * Tests JsonHttpTest. */ public class JsonHttpTestTest { private JsonHttpTest fixture = new JsonHttpTest(); @Test public void testFormatJson() { String expected = "<pre>{\n" + " "category": "reference",\n" + " "price": 8.95\n" + "}</pre>"; assertEquals(expected, fixture.safeFormatValue("{\"category\": \"reference\",\"price\": 8.95}").replace("\r", "")); assertEquals(expected, fixture.safeFormatValue(" {\"category\": \"reference\",\"price\": 8.95}").replace("\r", "")); } @Test public void testFormatUrl() { String expected = "http://myhost.com/get?has=a&hg=9223s"; assertEquals(expected, fixture.safeFormatValue(expected)); } @Test public void testJsonPathHandleEmptyResponse() { try { Object result = fixture.jsonPath("summary"); fail("Expected exception. Got: " + result); } catch (SlimFixtureException e) { checkException(e); } } @Test public void testJsonPathCountHandleEmptyResponse() { try { Object result = fixture.jsonPathCount("summary"); fail("Expected exception. Got: " + result); } catch (SlimFixtureException e) { checkException(e); } } private void checkException(SlimFixtureException e) { String msg = e.getMessage(); assertTrue("Did not expect a stacktrace for wiki" + msg, msg.startsWith("message:<<")); } @Test public void testDefaultContentType() { String contentType = fixture.getContentType(); assertEquals(HttpTest.DEFAULT_POST_CONTENT_TYPE, contentType); } @Test public void testDefaultJsonContentType() { String contentType = fixture.getContentTypeForJson(); assertEquals(ContentType.APPLICATION_JSON.toString(), contentType); String typeSet = "application/json"; fixture.setContentType(typeSet); contentType = fixture.getContentTypeForJson(); assertEquals(typeSet, contentType); } }