Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update: Expand test suite for multipart subscriptions #287

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ final class MultipartResponseSubscriptionParserTests: XCTestCase {

// MARK: - Error tests

func test__error__givenChunk_withIncorrectContentType_shouldReturnError() throws {
func test__error__givenIncorrectContentType_shouldReturnError() throws {
let subject = InterceptorTester(interceptor: MultipartResponseParsingInterceptor())

let expectation = expectation(description: "Received callback")
Expand Down Expand Up @@ -46,7 +46,7 @@ final class MultipartResponseSubscriptionParserTests: XCTestCase {
wait(for: [expectation], timeout: defaultTimeout)
}

func test__error__givenChunk_withTransportError_shouldReturnError() throws {
func test__error__givenTransportError_shouldReturnError() throws {
let subject = InterceptorTester(interceptor: MultipartResponseParsingInterceptor())

let expectation = expectation(description: "Received callback")
Expand Down Expand Up @@ -84,7 +84,7 @@ final class MultipartResponseSubscriptionParserTests: XCTestCase {
wait(for: [expectation], timeout: defaultTimeout)
}

func test__error__givenUnrecognizableChunk_shouldReturnError() throws {
func test__error__givenTransportErrorWithNullPayload_shouldReturnError() throws {
let subject = InterceptorTester(interceptor: MultipartResponseParsingInterceptor())

let expectation = expectation(description: "Received callback")
Expand All @@ -97,7 +97,14 @@ final class MultipartResponseSubscriptionParserTests: XCTestCase {
--graphql
content-type: application/json

not_a_valid_json_object
{
"payload": null,
"errors" : [
{
"message" : "forced test failure!"
}
]
}
--graphql
""".crlfFormattedData()
)
Expand All @@ -108,15 +115,59 @@ final class MultipartResponseSubscriptionParserTests: XCTestCase {

expect(result).to(beFailure { error in
expect(error).to(
matchError(MultipartResponseSubscriptionParser.ParsingError.cannotParseChunkData)
matchError(MultipartResponseSubscriptionParser.ParsingError.irrecoverableError(message: "forced test failure!"))
)
})
}

wait(for: [expectation], timeout: defaultTimeout)
}

func test__error__givenTransportErrorWithValidPayload_errorShouldTakePrecendenceAndReturnError() throws {
let subject = InterceptorTester(interceptor: MultipartResponseParsingInterceptor())

let expectation = expectation(description: "Received callback")

subject.intercept(
request: .mock(operation: MockSubscription.mock()),
response: .mock(
headerFields: ["Content-Type": "multipart/mixed;boundary=graphql;subscriptionSpec=1.0"],
data: """
--graphql
content-type: application/json

{
"payload": {
"data": {
"__typename": "Time",
"ticker": 1
}
},
"errors" : [
{
"message" : "forced test failure!"
}
]
}
--graphql
""".crlfFormattedData()
)
) { result in
defer {
expectation.fulfill()
}

expect(result).to(beFailure { error in
expect(error).to(
matchError(MultipartResponseSubscriptionParser.ParsingError.irrecoverableError(message: "forced test failure!"))
)
})
}

wait(for: [expectation], timeout: defaultTimeout)
}

func test__error__givenChunk_withMissingPayload_shouldReturnError() throws {
func test__error__givenTransportErrorIncludingUnknownKeys_shouldReturnErrorWithMessageOnly() throws {
let subject = InterceptorTester(interceptor: MultipartResponseParsingInterceptor())

let expectation = expectation(description: "Received callback")
Expand All @@ -130,7 +181,15 @@ final class MultipartResponseSubscriptionParserTests: XCTestCase {
content-type: application/json

{
"key": "value"
"errors" : [
{
"message" : "forced test failure!",
"path": [
"hello"
],
"foo": "bar"
}
]
}
--graphql
""".crlfFormattedData()
Expand All @@ -142,7 +201,39 @@ final class MultipartResponseSubscriptionParserTests: XCTestCase {

expect(result).to(beFailure { error in
expect(error).to(
matchError(MultipartResponseSubscriptionParser.ParsingError.cannotParsePayloadData)
matchError(MultipartResponseSubscriptionParser.ParsingError.irrecoverableError(message: "forced test failure!"))
)
})
}

wait(for: [expectation], timeout: defaultTimeout)
}

func test__error__givenMalformedJSON_shouldReturnError() throws {
let subject = InterceptorTester(interceptor: MultipartResponseParsingInterceptor())

let expectation = expectation(description: "Received callback")

subject.intercept(
request: .mock(operation: MockSubscription.mock()),
response: .mock(
headerFields: ["Content-Type": "multipart/mixed;boundary=graphql;subscriptionSpec=1.0"],
data: """
--graphql
content-type: application/json

not_a_valid_json_object
--graphql
""".crlfFormattedData()
)
) { result in
defer {
expectation.fulfill()
}

expect(result).to(beFailure { error in
expect(error).to(
matchError(MultipartResponseSubscriptionParser.ParsingError.cannotParseChunkData)
)
})
}
Expand Down Expand Up @@ -188,38 +279,114 @@ final class MultipartResponseSubscriptionParserTests: XCTestCase {
--graphql
content-type: application/json

{
"payload": {
"data": {
"__typename": "Time",
"ticker": 1
}
}
}
--graphql
content-type: application/json

{}
--graphql
content-type: application/json

{
"payload": {
"data": {
"__typename": "Time",
"ticker": 2
}
}
}
--graphql
""".crlfFormattedData()
)

let expectation = expectation(description: "Heartbeat ignored")
expectation.isInverted = true
expectation.expectedFulfillmentCount = 2

_ = network.send(operation: MockSubscription<Time>()) { result in
expectation.fulfill()
defer {
expectation.fulfill()
}

expect(result).to(beSuccess())
}

wait(for: [expectation], timeout: defaultTimeout)
}

func test__parsing__givenPayloadNull_shouldIgnore() throws {
func test__parsing__givenNullPayload_shouldIgnore() throws {
let network = buildNetworkTransport(responseData: """
--graphql
content-type: application/json
Content-Type: application/json

{
"payload": null
}
--graphql
Content-Type: application/json

{
"payload": {
"data": {
"__typename": "Time",
"ticker": 1
}
}
}
--graphql
""".crlfFormattedData()
)

let expectation = expectation(description: "Null payload ignored")

_ = network.send(operation: MockSubscription<Time>()) { result in
defer {
expectation.fulfill()
}

expect(result).to(beSuccess())
}

wait(for: [expectation], timeout: defaultTimeout)
calvincestari marked this conversation as resolved.
Show resolved Hide resolved
}

func test__parsing__givenNullErrors_shouldIgnore() throws {
calvincestari marked this conversation as resolved.
Show resolved Hide resolved
let network = buildNetworkTransport(responseData: """
--graphql
content-type: application/json

{
"errors": null
}
--graphql
content-type: application/json

{
"payload": {
"data": {
"__typename": "Time",
"ticker": 1
}
}
}
--graphql
""".crlfFormattedData()
)

let expectation = expectation(description: "Payload (null) ignored")
expectation.isInverted = true
let expectation = expectation(description: "Null errors ignored")

_ = network.send(operation: MockSubscription<Time>()) { result in
expectation.fulfill()
defer {
expectation.fulfill()
}

expect(result).to(beSuccess())
}

wait(for: [expectation], timeout: defaultTimeout)
Expand Down Expand Up @@ -318,7 +485,48 @@ final class MultipartResponseSubscriptionParserTests: XCTestCase {
wait(for: [expectation], timeout: defaultTimeout)
}

func test__parsing__givenChunkWithGraphQLError_shouldReturnSuccessWithGraphQLError() throws {
func test__parsing__givenPayloadAndUnknownKeys_shouldReturnSuccessAndIgnoreUnknownKeys() throws {
let network = buildNetworkTransport(responseData: """
--graphql
content-type: application/json

{
"foo": "bar",
"payload": {
"data": {
"__typename": "Time",
"ticker": 1
}
}
}
--graphql
""".crlfFormattedData()
)

let expectedData = try Time(data: [
"__typename": "Time",
"ticker": 1
], variables: nil)

let expectation = expectation(description: "Multipart data received")

_ = network.send(operation: MockSubscription<Time>()) { result in
defer {
expectation.fulfill()
}

switch (result) {
case let .success(data):
expect(data.data).to(equal(expectedData))
case let .failure(error):
fail("Unexpected failure result - \(error)")
}
}

wait(for: [expectation], timeout: defaultTimeout)
}

func test__parsing__givenPayloadWithDataAndGraphQLError_shouldReturnSuccessWithDataAndGraphQLError() throws {
let network = buildNetworkTransport(responseData: """
--graphql
content-type: application/json
Expand Down Expand Up @@ -364,6 +572,42 @@ final class MultipartResponseSubscriptionParserTests: XCTestCase {
wait(for: [expectation], timeout: defaultTimeout)
}

func test__parsing__givenPayloadWithNullDataAndGraphQLError_shouldReturnSuccessWithOnlyGraphQLError() throws {
let network = buildNetworkTransport(responseData: """
--graphql
content-type: application/json

{
"payload": {
"data": null,
"errors": [
{
"message": "test error"
}
]
}
}
--graphql
""".crlfFormattedData()
)

let expectation = expectation(description: "Multipart data received")

_ = network.send(operation: MockSubscription<Time>()) { result in
switch (result) {
case let .success(data):
expect(data.errors).to(equal([GraphQLError("test error")]))

expectation.fulfill()

case let .failure(error):
fail("Unexpected failure result - \(error)")
}
}

wait(for: [expectation], timeout: defaultTimeout)
}

func test__parsing__givenEndOfStream_shouldReturnSuccess() throws {
let network = buildNetworkTransport(responseData: """
--graphql
Expand Down
Loading
Loading