Skip to content

Commit

Permalink
Additional delimiter parsing tests
Browse files Browse the repository at this point in the history
  • Loading branch information
calvincestari committed Oct 4, 2024
1 parent cb6e28a commit 010f5f4
Show file tree
Hide file tree
Showing 2 changed files with 143 additions and 5 deletions.
98 changes: 95 additions & 3 deletions Tests/ApolloTests/DeferTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,11 @@ final class DeferTests: XCTestCase {
// MARK: Parsing tests

private func buildNetworkTransport(
responseData: Data
responseData: Data,
multipartBoundary boundary: String = "graphql"
) -> RequestChainNetworkTransport {
let client = MockURLSessionClient(
response: .mock(headerFields: ["Content-Type": "multipart/mixed;boundary=graphql;deferSpec=20220824"]),
response: .mock(headerFields: ["Content-Type": "multipart/mixed;boundary=\(boundary);deferSpec=20220824"]),
data: responseData
)

Expand Down Expand Up @@ -225,7 +226,7 @@ final class DeferTests: XCTestCase {
content-type: application/json
{
"hasNext": true,
"hasNext": false,
"incremental": [
{
"label": "deferredGenres",
Expand Down Expand Up @@ -445,4 +446,95 @@ final class DeferTests: XCTestCase {
wait(for: [expectation], timeout: defaultTimeout)
}

func test__parsing__givenPartialAndIncrementalResponses_withDashBoundaryInMessageBody_shouldNotSplitChunk() throws {
let multipartBoundary = "-"
let mysteryCharacterName = "lots\(multipartBoundary)of-\(multipartBoundary)similar--\(multipartBoundary)boundaries---\(multipartBoundary)in----\(multipartBoundary)this-----\(multipartBoundary)string"

let network = buildNetworkTransport(
responseData: """
--\(multipartBoundary)
content-type: application/json
{
"hasNext": true,
"data": {
"show" : {
"__typename": "show",
"name": "The Scooby-Doo Show",
"characters": [
{
"__typename": "Character",
"name": "Scooby-Doo"
},
{
"__typename": "Character",
"name": "Shaggy Rogers"
},
{
"__typename": "Character",
"name": "Velma Dinkley"
},
{
"__typename": "Character",
"name": "\(mysteryCharacterName)"
}
]
}
}
}
--\(multipartBoundary)
content-type: application/json
{
"hasNext": false,
"incremental": [
{
"label": "deferredGenres",
"path": [
"show"
],
"data": {
"genres": [
"Comedy",
"Mystery",
"Adventure"
]
}
}
]
}
--\(multipartBoundary)--
""".crlfFormattedData(),
multipartBoundary: multipartBoundary
)

let expectation = expectation(description: "Result received")
expectation.expectedFulfillmentCount = 2

_ = network.send(operation: TVShowQuery()) { result in
defer {
expectation.fulfill()
}

expect(result).to(beSuccess())

let data = try? result.get().data
expect(data?.__data._fulfilledFragments).to(equal([
ObjectIdentifier(TVShowQuery.Data.self),
]))
expect(data?.__data._deferredFragments).to(beEmpty())

let show = data?.show
if expectation.numberOfFulfillments == 0 { // Partial data
expect(show?.characters).to(haveCount(4))

let mysteryCharacter = show?.characters[3]
expect(mysteryCharacter?.name).to(equal(mysteryCharacterName))
}
}

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

}
Original file line number Diff line number Diff line change
Expand Up @@ -261,10 +261,11 @@ final class MultipartResponseSubscriptionParserTests: XCTestCase {
}

private func buildNetworkTransport(
responseData: Data
responseData: Data,
multipartBoundary boundary: String = "graphql"
) -> RequestChainNetworkTransport {
let client = MockURLSessionClient(
response: .mock(headerFields: ["Content-Type": "multipart/mixed;boundary=graphql;subscriptionSpec=1.0"]),
response: .mock(headerFields: ["Content-Type": "multipart/mixed;boundary=\(boundary);subscriptionSpec=1.0"]),
data: responseData
)

Expand Down Expand Up @@ -474,6 +475,51 @@ final class MultipartResponseSubscriptionParserTests: XCTestCase {
wait(for: [expectation], timeout: defaultTimeout)
}

func test__parsing__givenSingleChunk_withDashBoundaryInMessageBody_shouldReturnSuccess() throws {
let multipartBoundary = "-"
let network = buildNetworkTransport(
responseData: """
--\(multipartBoundary)
content-type: application/json
{
"payload": {
"data": {
"__typename": "Time",
"ticker": 1,
"description": "lots\(multipartBoundary)of-\(multipartBoundary)similar--\(multipartBoundary)boundaries---\(multipartBoundary)in----\(multipartBoundary)this-----\(multipartBoundary)string"
}
}
}
--\(multipartBoundary)--
""".crlfFormattedData(),
multipartBoundary: multipartBoundary
)

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__givenMultipleChunks_shouldReturnMultipleSuccesses() throws {
let network = buildNetworkTransport(responseData: """
Expand Down

0 comments on commit 010f5f4

Please sign in to comment.