99import com .sun .net .httpserver .HttpServer ;
1010
1111import java .io .IOException ;
12+ import java .io .InputStream ;
1213import java .io .OutputStream ;
1314import java .net .HttpURLConnection ;
1415import java .net .InetSocketAddress ;
1516import java .nio .ByteBuffer ;
1617import java .nio .charset .StandardCharsets ;
1718
19+ import org .apache .commons .io .IOUtils ;
1820import org .apache .hadoop .conf .Configuration ;
1921import org .eclipse .rdf4j .model .Literal ;
2022import org .eclipse .rdf4j .query .BindingSet ;
@@ -52,7 +54,7 @@ public void simpleRequestTest() throws Exception {
5254 try (RepositoryConnection conn = hbaseRepo .getConnection ()) {
5355 TupleQuery q = conn .prepareTupleQuery (
5456 "PREFIX halyard: <http://merck.github.io/Halyard/ns#> PREFIX http: <http://www.w3.org/2011/http#> select * { [] a http:Request; http:absoluteURI '" + server .getUrl ()
55- + "'; http:resp [http:statusCodeValue ?sc; http:body ?body ] }" );
57+ + "'; http:resp [http:statusCodeValue ?sc; http:reasonPhrase ?reason; http: body ?body ] }" );
5658 try (TupleQueryResult iter = q .evaluate ()) {
5759 assertTrue (iter .hasNext ());
5860 BindingSet bs = iter .next ();
@@ -73,7 +75,7 @@ public void headerRequestTest() throws Exception {
7375 Repository hbaseRepo = createRepo ("testSimpleRequest" );
7476 try (RepositoryConnection conn = hbaseRepo .getConnection ()) {
7577 TupleQuery q = conn .prepareTupleQuery ("PREFIX halyard: <http://merck.github.io/Halyard/ns#> PREFIX http: <http://www.w3.org/2011/http#> select * { [] a http:Request; http:absoluteURI '" + server .getUrl ()
76- + "'; http:headers ([http:fieldName 'User-agent'; http:fieldValue '" + userAgent + "']); http:resp [http:statusCodeValue ?sc; http:body ?body ] }" );
78+ + "'; http:headers ([http:fieldName 'User-agent'; http:fieldValue '" + userAgent + "']); http:resp [http:statusCodeValue ?sc; http:reasonPhrase ?reason; http: body ?body ] }" );
7779 try (TupleQueryResult iter = q .evaluate ()) {
7880 assertTrue (iter .hasNext ());
7981 BindingSet bs = iter .next ();
@@ -90,6 +92,34 @@ public void headerRequestTest() throws Exception {
9092 }
9193 }
9294
95+ @ Test
96+ public void postRequestTest () throws Exception {
97+ String contentType = "text/plain" ;
98+ String requestBody = "foobar" ;
99+ String expectedBody = "{\" msg\" :\" Hi!\" }" ;
100+ try (MockHttpServer server = startHttpServer ("application/json" , toBytes (expectedBody ))) {
101+ Repository hbaseRepo = createRepo ("testSimpleRequest" );
102+ try (RepositoryConnection conn = hbaseRepo .getConnection ()) {
103+ TupleQuery q = conn .prepareTupleQuery ("PREFIX halyard: <http://merck.github.io/Halyard/ns#> PREFIX http: <http://www.w3.org/2011/http#> select * { [] a http:Request; http:absoluteURI '" + server .getUrl ()
104+ + "'; http:headers ([http:fieldName 'Content-type'; http:fieldValue '" + contentType + "']); http:body '" + requestBody + "'; http:resp [http:statusCodeValue ?sc; http:reasonPhrase ?reason; http:body ?body ] }" );
105+ try (TupleQueryResult iter = q .evaluate ()) {
106+ assertTrue (iter .hasNext ());
107+ BindingSet bs = iter .next ();
108+ assertNotNull (server .requestHeaders );
109+ assertEquals (contentType , server .requestHeaders .get ("Content-type" ).get (0 ));
110+ assertNotNull (server .requestBody );
111+ assertArrayEquals (toBytes (requestBody ), server .requestBody );
112+ assertEquals (200 , ((Literal ) bs .getValue ("sc" )).intValue ());
113+ Literal actualBody = (Literal ) bs .getValue ("body" );
114+ assertEquals (expectedBody , actualBody .stringValue ());
115+ assertEquals (HALYARD .MAP_TYPE , actualBody .getDatatype ());
116+ assertFalse (iter .hasNext ());
117+ }
118+ }
119+ hbaseRepo .shutDown ();
120+ }
121+ }
122+
93123 private MockHttpServer startHttpServer (String contentType , byte [] response ) throws IOException {
94124 MockHttpServer server = new MockHttpServer (contentType , response );
95125 server .start ();
@@ -106,13 +136,17 @@ private static byte[] toBytes(String s) {
106136 static class MockHttpServer implements AutoCloseable {
107137 final HttpServer server ;
108138 Headers requestHeaders ;
139+ byte [] requestBody ;
109140
110141 MockHttpServer (String contentType , byte [] response ) throws IOException {
111142 server = HttpServer .create (new InetSocketAddress ("localhost" , 0 ), 0 );
112143 server .createContext ("/" , new HttpHandler () {
113144 @ Override
114145 public void handle (HttpExchange he ) throws IOException {
115146 requestHeaders = he .getRequestHeaders ();
147+ try (InputStream in = he .getRequestBody ()) {
148+ requestBody = IOUtils .toByteArray (in );
149+ }
116150 he .getResponseHeaders ().add ("Content-type" , contentType );
117151 he .sendResponseHeaders (HttpURLConnection .HTTP_OK , 0 );
118152 try (OutputStream out = he .getResponseBody ()) {
0 commit comments