Skip to content

Commit

Permalink
Added a test for X-FORWARD-HOST headers in the transactional endpoint.
Browse files Browse the repository at this point in the history
Minor refactoring to allow this test to use some existing helper methods.
  • Loading branch information
jimwebber committed Feb 16, 2015
1 parent 31537ad commit 49f08ae
Showing 1 changed file with 73 additions and 31 deletions.
Expand Up @@ -19,9 +19,6 @@
*/ */
package org.neo4j.server.rest.transactional.integration; package org.neo4j.server.rest.transactional.integration;


import org.codehaus.jackson.JsonNode;
import org.junit.Test;

import java.io.InputStream; import java.io.InputStream;
import java.io.InputStreamReader; import java.io.InputStreamReader;
import java.io.PrintStream; import java.io.PrintStream;
Expand All @@ -35,13 +32,17 @@
import java.util.concurrent.Executors; import java.util.concurrent.Executors;
import java.util.concurrent.Future; import java.util.concurrent.Future;


import org.codehaus.jackson.JsonNode;
import org.junit.Test;

import org.neo4j.graphdb.Node; import org.neo4j.graphdb.Node;
import org.neo4j.graphdb.Transaction; import org.neo4j.graphdb.Transaction;
import org.neo4j.kernel.GraphDatabaseAPI; import org.neo4j.kernel.GraphDatabaseAPI;
import org.neo4j.kernel.api.exceptions.Status; import org.neo4j.kernel.api.exceptions.Status;
import org.neo4j.kernel.impl.transaction.TransactionCounters; import org.neo4j.kernel.impl.transaction.TransactionCounters;
import org.neo4j.server.ServerTestUtils; import org.neo4j.server.ServerTestUtils;
import org.neo4j.server.rest.AbstractRestFunctionalTestBase; import org.neo4j.server.rest.AbstractRestFunctionalTestBase;
import org.neo4j.server.web.XForwardUtil;
import org.neo4j.test.server.HTTP; import org.neo4j.test.server.HTTP;
import org.neo4j.test.server.HTTP.Response; import org.neo4j.test.server.HTTP.Response;
import org.neo4j.tooling.GlobalGraphOperations; import org.neo4j.tooling.GlobalGraphOperations;
Expand Down Expand Up @@ -105,7 +106,7 @@ public void begin__execute__rollback() throws Exception
assertHasTxLocation( begin ); assertHasTxLocation( begin );


// execute // execute
http.POST( begin.location(), quotedJson( "{ 'statements': [ { 'statement': 'CREATE n' } ] }" ) ); http.POST( begin.location(), quotedJson( "{ 'statements': [ { 'statement': 'CREATE n' } ] }" ) );


// rollback // rollback
Response commit = http.DELETE( begin.location() ); Response commit = http.DELETE( begin.location() );
Expand Down Expand Up @@ -217,7 +218,7 @@ public void returned_rest_urls_must_be_useable() throws Exception
{ {
// begin and execute and commit "resultDataContents":["REST"] // begin and execute and commit "resultDataContents":["REST"]
HTTP.RawPayload payload = quotedJson( "{ 'statements': [ { 'statement': 'CREATE (n {a: 1}) return n', " + HTTP.RawPayload payload = quotedJson( "{ 'statements': [ { 'statement': 'CREATE (n {a: 1}) return n', " +
"'resultDataContents' : ['REST'] } ] }" ); "'resultDataContents' : ['REST'] } ] }" );
Response begin = http.POST( "/db/data/transaction/commit", payload ); Response begin = http.POST( "/db/data/transaction/commit", payload );


assertThat( begin.status(), equalTo( 200 ) ); assertThat( begin.status(), equalTo( 200 ) );
Expand Down Expand Up @@ -304,7 +305,8 @@ public void execute( String url ) throws Exception
@Test @Test
public void begin_and_execute_cypher_21_periodic_commit_that_returns_data_and_commit() throws Exception public void begin_and_execute_cypher_21_periodic_commit_that_returns_data_and_commit() throws Exception
{ {
ServerTestUtils.withCSVFile( 1, new ServerTestUtils.BlockWithCSVFileURL() { ServerTestUtils.withCSVFile( 1, new ServerTestUtils.BlockWithCSVFileURL()
{
@Override @Override
public void execute( String url ) throws Exception public void execute( String url ) throws Exception
{ {
Expand All @@ -313,17 +315,18 @@ public void execute( String url ) throws Exception
// begin and execute and commit // begin and execute and commit
Response response = http.POST( Response response = http.POST(
"/db/data/transaction/commit", "/db/data/transaction/commit",
quotedJson( "{ 'statements': [ { 'statement': 'CYPHER 2.1 USING PERIODIC COMMIT LOAD CSV FROM \\\"" + url + "\\\" AS line CREATE (n {id: 23}) RETURN n' } ] }" ) quotedJson( "{ 'statements': [ { 'statement': 'CYPHER 2.1 USING PERIODIC COMMIT LOAD CSV FROM" +
" \\\"" + url + "\\\" AS line CREATE (n {id: 23}) RETURN n' } ] }" )
); );


assertThat( response.status(), equalTo( 200 ) ); assertThat( response.status(), equalTo( 200 ) );


assertThat( response, containsNoErrors() ); assertThat( response, containsNoErrors() );


JsonNode columns = response.get( "results" ).get( 0 ).get( "columns" ); JsonNode columns = response.get( "results" ).get( 0 ).get( "columns" );
assertThat(columns.toString(), equalTo("[\"n\"]")); assertThat( columns.toString(), equalTo( "[\"n\"]" ) );


assertThat(countNodes(), equalTo(nodesInDatabaseBeforeTransaction + 1)); assertThat( countNodes(), equalTo( nodesInDatabaseBeforeTransaction + 1 ) );
} }
} ); } );
} }
Expand Down Expand Up @@ -756,42 +759,81 @@ public void shouldSerializeMapsCorrectlyInRestFormat() throws Exception
assertThat( response.get( "errors" ).size(), equalTo( 0 ) ); assertThat( response.get( "errors" ).size(), equalTo( 0 ) );
} }


static PrintStream out = System.err;
@Test @Test
public void rest_format_nodes_should_have_sensible_uris() throws Throwable public void restFormatNodesShouldHaveSensibleUris() throws Throwable
{ {
// given
final String hostname = "localhost";
final String scheme = "http";

// when // when
Response rs = http.POST( "/db/data/transaction/commit", quotedJson( Response rs = http.POST( "/db/data/transaction/commit", quotedJson(
"{ 'statements': [ { 'statement': 'CREATE (n:Foo:Bar) RETURN n', 'resultDataContents':['rest'] } ] }" "{ 'statements': [ { 'statement': 'CREATE (n:Foo:Bar) RETURN n', 'resultDataContents':['rest'] } ] }"
)); ) );

// then
JsonNode restNode = rs.get( "results" ).get( 0 ).get( "data" ).get( 0 ).get( "rest" ).get( 0 );

assertPath( restNode.get( "labels" ), "/node/\\d+/labels", hostname, scheme );
assertPath( restNode.get( "outgoing_relationships" ), "/node/\\d+/relationships/out", hostname, scheme );
assertPath( restNode.get( "traverse" ), "/node/\\d+/traverse/\\{returnType\\}", hostname, scheme );
assertPath( restNode.get( "all_typed_relationships" ),
"/node/\\d+/relationships/all/\\{-list\\|&\\|types\\}", hostname, scheme );
assertPath( restNode.get( "self" ), "/node/\\d+", hostname, scheme );
assertPath( restNode.get( "property" ), "/node/\\d+/properties/\\{key\\}", hostname, scheme );
assertPath( restNode.get( "properties" ), "/node/\\d+/properties", hostname, scheme );
assertPath( restNode.get( "outgoing_typed_relationships" ),
"/node/\\d+/relationships/out/\\{-list\\|&\\|types\\}", hostname, scheme );
assertPath( restNode.get( "incoming_relationships" ), "/node/\\d+/relationships/in", hostname, scheme );
assertPath( restNode.get( "create_relationship" ), "/node/\\d+/relationships", hostname, scheme );
assertPath( restNode.get( "paged_traverse" ), "/node/\\d+/paged/traverse/\\{returnType\\}\\{\\?pageSize," +
"leaseTime\\}", "localhost", scheme );
assertPath( restNode.get( "all_relationships" ), "/node/\\d+/relationships/all", hostname, scheme );
assertPath( restNode.get( "incoming_typed_relationships" ),
"/node/\\d+/relationships/in/\\{-list\\|&\\|types\\}", hostname, scheme );
}

@Test
public void restFormattedNodesShouldHaveSensibleUrisWhenUsingXForwardHeader() throws Throwable
{
// given
final String hostname = "dummy.example.org";
final String scheme = "http";

// when
Response rs = http.withHeaders( XForwardUtil.X_FORWARD_HOST_HEADER_KEY, hostname )
.POST( "/db/data/transaction/commit", quotedJson(
"{ 'statements': [ { 'statement': 'CREATE (n:Foo:Bar) RETURN n', " +
"'resultDataContents':['rest'] } ] }"
) );


// then // then
JsonNode restNode = rs.get( "results" ).get(0).get( "data" ).get( 0 ).get( "rest" ).get(0); JsonNode restNode = rs.get( "results" ).get( 0 ).get( "data" ).get( 0 ).get( "rest" ).get( 0 );


assertPath( restNode.get("labels"), "/node/\\d+/labels" ); assertPath( restNode.get( "labels" ), "/node/\\d+/labels", hostname, scheme );
assertPath( restNode.get("outgoing_relationships"), "/node/\\d+/relationships/out" ); assertPath( restNode.get( "outgoing_relationships" ), "/node/\\d+/relationships/out", hostname, scheme );
assertPath( restNode.get( "traverse" ), "/node/\\d+/traverse/\\{returnType\\}" ); assertPath( restNode.get( "traverse" ), "/node/\\d+/traverse/\\{returnType\\}", hostname, scheme );
assertPath( restNode.get( "all_typed_relationships" ), assertPath( restNode.get( "all_typed_relationships" ),
"/node/\\d+/relationships/all/\\{-list\\|&\\|types\\}" ); "/node/\\d+/relationships/all/\\{-list\\|&\\|types\\}", hostname, scheme );
assertPath( restNode.get( "self" ), "/node/\\d+" ); assertPath( restNode.get( "self" ), "/node/\\d+", hostname, scheme );
assertPath( restNode.get( "property" ), "/node/\\d+/properties/\\{key\\}" ); assertPath( restNode.get( "property" ), "/node/\\d+/properties/\\{key\\}", hostname, scheme );
assertPath( restNode.get( "properties" ), "/node/\\d+/properties" ); assertPath( restNode.get( "properties" ), "/node/\\d+/properties", hostname, scheme );
assertPath( restNode.get( "outgoing_typed_relationships" ), assertPath( restNode.get( "outgoing_typed_relationships" ),
"/node/\\d+/relationships/out/\\{-list\\|&\\|types\\}"); "/node/\\d+/relationships/out/\\{-list\\|&\\|types\\}", hostname, scheme );
assertPath( restNode.get( "incoming_relationships" ), "/node/\\d+/relationships/in" ); assertPath( restNode.get( "incoming_relationships" ), "/node/\\d+/relationships/in", hostname, scheme );
assertPath( restNode.get( "create_relationship" ), "/node/\\d+/relationships" ); assertPath( restNode.get( "create_relationship" ), "/node/\\d+/relationships", hostname, scheme );
assertPath( restNode.get( "paged_traverse" ), "/node/\\d+/paged/traverse/\\{returnType\\}\\{\\?pageSize," + assertPath( restNode.get( "paged_traverse" ), "/node/\\d+/paged/traverse/\\{returnType\\}\\{\\?pageSize," +
"leaseTime\\}"); "leaseTime\\}", hostname, scheme );
assertPath( restNode.get( "all_relationships" ), "/node/\\d+/relationships/all" ); assertPath( restNode.get( "all_relationships" ), "/node/\\d+/relationships/all", hostname, scheme );
assertPath( restNode.get( "incoming_typed_relationships" ), assertPath( restNode.get( "incoming_typed_relationships" ),
"/node/\\d+/relationships/in/\\{-list\\|&\\|types\\}"); "/node/\\d+/relationships/in/\\{-list\\|&\\|types\\}", hostname, scheme );
} }


private void assertPath( JsonNode jsonURIString, String path ) private void assertPath( JsonNode jsonURIString, String path, String hostname, final String scheme )
{ {
assertTrue("Expected a uri matching 'http://localhost:\\d+/db/data" + path + "', " + assertTrue( "Expected a uri matching '" + scheme + "://" + hostname + ":\\d+/db/data" + path + "', " +
"but got '" + jsonURIString.asText() + "'.", "but got '" + jsonURIString.asText() + "'.",
jsonURIString.asText().matches( "http://localhost:\\d+/db/data" + path )); jsonURIString.asText().matches( scheme + "://" + hostname + ":\\d+/db/data" + path ) );
} }




Expand Down

0 comments on commit 49f08ae

Please sign in to comment.