/** * Test sending of HTTP GET requests. * * @throws Exception exception */ @Test public void testPOSTGet() throws Exception { // GET1 - just send a GET request final Command get1 = new XQuery( "http:send-request(" + "<http:request method='get' " + "href='http://localhost:8984/basex/jax-rx/books'/>)"); get1.execute(context); checkResponse(get1, HttpURLConnection.HTTP_OK, 2); assertTrue(((ItemIter) get1.result()).item[1].type == Type.DOC); // GET2 - with override-media-type='text/plain' final Command get2 = new XQuery( "http:send-request(" + "<http:request method='get' override-media-type='text/plain'/>," + "'http://localhost:8984/basex/jax-rx/books')"); get2.execute(context); checkResponse(get2, HttpURLConnection.HTTP_OK, 2); assertTrue(((ItemIter) get2.result()).item[1].type == Type.STR); // Get3 - with status-only='true' final Command get3 = new XQuery( "http:send-request(" + "<http:request method='get' status-only='true'/>," + "'http://localhost:8984/basex/jax-rx/books')"); get3.execute(context); checkResponse(get3, HttpURLConnection.HTTP_OK, 1); }
/** * Checks the response to an HTTP request. * * @param c command * @param expStatus expected status * @param itemsCount expected number of items * @throws QueryException query exception */ private void checkResponse(final Command c, final int expStatus, final int itemsCount) throws QueryException { assertTrue(c.result() instanceof ItemIter); final ItemIter res = (ItemIter) c.result(); assertEquals(itemsCount, res.size()); assertTrue(res.item[0] instanceof FElem); final FElem response = (FElem) res.item[0]; assertNotNull(response.attr()); final NodeIter resAttr = response.attr(); Nod attr = null; while ((attr = resAttr.next()) != null) { if (Token.eq(attr.nname(), STATUS)) assertTrue(eq(attr.atom(), token(expStatus))); } }