-
Notifications
You must be signed in to change notification settings - Fork 114
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #519 from graphql-java-kickstart/feature/516-webso…
…cket-origin fix(#516): add origin check to websockets
- Loading branch information
Showing
3 changed files
with
127 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
83 changes: 83 additions & 0 deletions
83
...java-servlet/src/test/groovy/graphql/kickstart/servlet/GraphQLWebsocketServletSpec.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
package graphql.kickstart.servlet | ||
|
||
import spock.lang.Specification | ||
|
||
class GraphQLWebsocketServletSpec extends Specification { | ||
|
||
def "checkOrigin without any allowed origins allows given origin"() { | ||
given: "a websocket servlet with no allowed origins" | ||
def servlet = new GraphQLWebsocketServlet(GraphQLConfiguration.with(TestUtils.createGraphQlSchema()).build()) | ||
|
||
when: "we check origin http://localhost:8080" | ||
def allowed = servlet.checkOrigin("http://localhost:8080") | ||
|
||
then: | ||
allowed | ||
} | ||
|
||
def "checkOrigin without any allowed origins allows when no origin given"() { | ||
given: "a websocket servlet with no allowed origins" | ||
def servlet = new GraphQLWebsocketServlet(GraphQLConfiguration.with(TestUtils.createGraphQlSchema()).build()) | ||
|
||
when: "we check origin null" | ||
def allowed = servlet.checkOrigin(null) | ||
|
||
then: | ||
allowed | ||
} | ||
|
||
def "checkOrigin without any allowed origins allows when origin is empty"() { | ||
given: "a websocket servlet with no allowed origins" | ||
def servlet = new GraphQLWebsocketServlet(GraphQLConfiguration.with(TestUtils.createGraphQlSchema()).build()) | ||
|
||
when: "we check origin null" | ||
def allowed = servlet.checkOrigin(" ") | ||
|
||
then: | ||
allowed | ||
} | ||
|
||
def "checkOrigin with allow all origins allows given origin"() { | ||
given: "a websocket servlet with allow all origins" | ||
def servlet = new GraphQLWebsocketServlet(GraphQLConfiguration.with(TestUtils.createGraphQlSchema()).allowedOrigins(List.of("*")).build()) | ||
|
||
when: "we check origin http://localhost:8080" | ||
def allowed = servlet.checkOrigin("http://localhost:8080") | ||
|
||
then: | ||
allowed | ||
} | ||
|
||
def "checkOrigin with specific allowed origins allows given origin"() { | ||
given: "a websocket servlet with allow all origins" | ||
def servlet = new GraphQLWebsocketServlet(GraphQLConfiguration.with(TestUtils.createGraphQlSchema()).allowedOrigins(List.of("http://localhost:8080")).build()) | ||
|
||
when: "we check origin http://localhost:8080" | ||
def allowed = servlet.checkOrigin("http://localhost:8080") | ||
|
||
then: | ||
allowed | ||
} | ||
|
||
def "checkOrigin with specific allowed origins allows given origin with trailing slash"() { | ||
given: "a websocket servlet with allow all origins" | ||
def servlet = new GraphQLWebsocketServlet(GraphQLConfiguration.with(TestUtils.createGraphQlSchema()).allowedOrigins(List.of("http://localhost:8080")).build()) | ||
|
||
when: "we check origin http://localhost:8080/" | ||
def allowed = servlet.checkOrigin("http://localhost:8080/") | ||
|
||
then: | ||
allowed | ||
} | ||
|
||
def "checkOrigin with specific allowed origins with trailing slash allows given origin without trailing slash"() { | ||
given: "a websocket servlet with allow all origins" | ||
def servlet = new GraphQLWebsocketServlet(GraphQLConfiguration.with(TestUtils.createGraphQlSchema()).allowedOrigins(List.of("http://localhost:8080/")).build()) | ||
|
||
when: "we check origin http://localhost:8080" | ||
def allowed = servlet.checkOrigin("http://localhost:8080") | ||
|
||
then: | ||
allowed | ||
} | ||
} |