|
| 1 | +import XCTest |
| 2 | +import Nimble |
| 3 | +@testable import Apollo |
| 4 | +import ApolloAPI |
| 5 | +import ApolloInternalTestHelpers |
| 6 | + |
| 7 | +class RequestClientMetadataTests : XCTestCase { |
| 8 | + |
| 9 | + private class Hero: MockSelectionSet { |
| 10 | + typealias Schema = MockSchemaMetadata |
| 11 | + |
| 12 | + override class var __selections: [Selection] {[ |
| 13 | + .field("__typename", String.self), |
| 14 | + .field("name", String.self) |
| 15 | + ]} |
| 16 | + |
| 17 | + var name: String { __data["name"] } |
| 18 | + } |
| 19 | + |
| 20 | + // MARK: JSONRequest |
| 21 | + |
| 22 | + func test__jsonRequest__usingDefaultInitializer_shouldAddClientHeadersAndExtension() throws { |
| 23 | + let jsonRequest = JSONRequest( |
| 24 | + operation: MockQuery<Hero>(), |
| 25 | + graphQLEndpoint: TestURL.mockServer.url, |
| 26 | + clientName: "test-client", |
| 27 | + clientVersion: "test-client-version" |
| 28 | + ) |
| 29 | + |
| 30 | + let urlRequest = try jsonRequest.toURLRequest() |
| 31 | + |
| 32 | + guard let httpHeaderFields = urlRequest.allHTTPHeaderFields else { |
| 33 | + fail("Missing HTTP header fields!") |
| 34 | + return |
| 35 | + } |
| 36 | + |
| 37 | + expect(httpHeaderFields["apollographql-client-version"]).to(equal("test-client-version")) |
| 38 | + expect(httpHeaderFields["apollographql-client-name"]).to(equal("test-client")) |
| 39 | + |
| 40 | + guard |
| 41 | + let httpBody = urlRequest.httpBody, |
| 42 | + let jsonBody = try? JSONSerialization.jsonObject(with: httpBody) as? JSONObject, |
| 43 | + let extensions = jsonBody["extensions"] as? JSONObject, |
| 44 | + let clientLibrary = extensions["clientLibrary"] as? JSONObject |
| 45 | + else { |
| 46 | + fail("Could not deserialize client library extension.") |
| 47 | + return |
| 48 | + } |
| 49 | + |
| 50 | + expect(clientLibrary["name"]).to(equal(Constants.ApolloClientName)) |
| 51 | + expect(clientLibrary["version"]).to(equal(Constants.ApolloClientVersion)) |
| 52 | + } |
| 53 | + |
| 54 | + func test__jsonRequest__usingInitializerEnablingClientExtension_shouldAddClientHeadersAndExtension() throws { |
| 55 | + let jsonRequest = JSONRequest( |
| 56 | + operation: MockQuery<Hero>(), |
| 57 | + graphQLEndpoint: TestURL.mockServer.url, |
| 58 | + clientName: "test-client", |
| 59 | + clientVersion: "test-client-version", |
| 60 | + sendEnhancedClientAwareness: true |
| 61 | + ) |
| 62 | + |
| 63 | + let urlRequest = try jsonRequest.toURLRequest() |
| 64 | + |
| 65 | + guard let httpHeaderFields = urlRequest.allHTTPHeaderFields else { |
| 66 | + fail("Missing HTTP header fields!") |
| 67 | + return |
| 68 | + } |
| 69 | + |
| 70 | + expect(httpHeaderFields["apollographql-client-version"]).to(equal("test-client-version")) |
| 71 | + expect(httpHeaderFields["apollographql-client-name"]).to(equal("test-client")) |
| 72 | + |
| 73 | + guard |
| 74 | + let httpBody = urlRequest.httpBody, |
| 75 | + let jsonBody = try? JSONSerialization.jsonObject(with: httpBody) as? JSONObject, |
| 76 | + let extensions = jsonBody["extensions"] as? JSONObject, |
| 77 | + let clientLibrary = extensions["clientLibrary"] as? JSONObject |
| 78 | + else { |
| 79 | + fail("Could not deserialize client library extension.") |
| 80 | + return |
| 81 | + } |
| 82 | + |
| 83 | + expect(clientLibrary["name"]).to(equal(Constants.ApolloClientName)) |
| 84 | + expect(clientLibrary["version"]).to(equal(Constants.ApolloClientVersion)) |
| 85 | + } |
| 86 | + |
| 87 | + func test__jsonRequest__usingInitializerDisablingClientExtension_shouldAddClientHeaders_doesNotAddClientExtension() throws { |
| 88 | + let jsonRequest = JSONRequest( |
| 89 | + operation: MockQuery<Hero>(), |
| 90 | + graphQLEndpoint: TestURL.mockServer.url, |
| 91 | + clientName: "test-client", |
| 92 | + clientVersion: "test-client-version", |
| 93 | + sendEnhancedClientAwareness: false |
| 94 | + ) |
| 95 | + |
| 96 | + let urlRequest = try jsonRequest.toURLRequest() |
| 97 | + |
| 98 | + guard let httpHeaderFields = urlRequest.allHTTPHeaderFields else { |
| 99 | + fail("Missing HTTP header fields!") |
| 100 | + return |
| 101 | + } |
| 102 | + |
| 103 | + expect(httpHeaderFields["apollographql-client-version"]).to(equal("test-client-version")) |
| 104 | + expect(httpHeaderFields["apollographql-client-name"]).to(equal("test-client")) |
| 105 | + |
| 106 | + guard |
| 107 | + let httpBody = urlRequest.httpBody, |
| 108 | + let jsonBody = try? JSONSerialization.jsonObject(with: httpBody) as? JSONObject |
| 109 | + else { |
| 110 | + fail("Could not deserialize client library extension.") |
| 111 | + return |
| 112 | + } |
| 113 | + |
| 114 | + expect(jsonBody["extensions"]).to(beNil()) |
| 115 | + } |
| 116 | + |
| 117 | + // MARK: UploadRequest |
| 118 | + |
| 119 | + func test__uploadRequest__usingDefaultInitializer_shouldAddClientHeadersAndExtension() throws { |
| 120 | + let uploadRequest = UploadRequest( |
| 121 | + graphQLEndpoint: TestURL.mockServer.url, |
| 122 | + operation: MockQuery<Hero>(), |
| 123 | + clientName: "test-client", |
| 124 | + clientVersion: "test-client-version", |
| 125 | + files: [GraphQLFile(fieldName: "x", originalName: "y", data: "z".data(using: .utf8)!)] |
| 126 | + ) |
| 127 | + |
| 128 | + let urlRequest = try uploadRequest.toURLRequest() |
| 129 | + |
| 130 | + guard let httpHeaderFields = urlRequest.allHTTPHeaderFields else { |
| 131 | + fail("Missing HTTP header fields!") |
| 132 | + return |
| 133 | + } |
| 134 | + |
| 135 | + expect(httpHeaderFields["apollographql-client-version"]).to(equal("test-client-version")) |
| 136 | + expect(httpHeaderFields["apollographql-client-name"]).to(equal("test-client")) |
| 137 | + |
| 138 | + guard |
| 139 | + let httpBody = urlRequest.httpBody, |
| 140 | + let multipartBody = String(data: httpBody, encoding: .utf8) |
| 141 | + else { |
| 142 | + fail("Could not deserialize client library extension.") |
| 143 | + return |
| 144 | + } |
| 145 | + |
| 146 | + expect(multipartBody).to(contain("{\"extensions\":{\"clientLibrary\":{\"name\":\"apollo-ios\",\"version\":\"\(Constants.ApolloClientVersion)\"}},\"operationName\":\"MockOperationName\",\"query\":\"Mock Operation Definition\",\"variables\":{\"x\":null}}")) |
| 147 | + } |
| 148 | + |
| 149 | + func test__uploadRequest__usingInitializerEnablingClientExtension_shouldAddClientHeadersAndExtension() throws { |
| 150 | + let uploadRequest = UploadRequest( |
| 151 | + graphQLEndpoint: TestURL.mockServer.url, |
| 152 | + operation: MockQuery<Hero>(), |
| 153 | + clientName: "test-client", |
| 154 | + clientVersion: "test-client-version", |
| 155 | + files: [GraphQLFile(fieldName: "x", originalName: "y", data: "z".data(using: .utf8)!)], |
| 156 | + sendEnhancedClientAwareness: true |
| 157 | + ) |
| 158 | + |
| 159 | + let urlRequest = try uploadRequest.toURLRequest() |
| 160 | + |
| 161 | + guard let httpHeaderFields = urlRequest.allHTTPHeaderFields else { |
| 162 | + fail("Missing HTTP header fields!") |
| 163 | + return |
| 164 | + } |
| 165 | + |
| 166 | + expect(httpHeaderFields["apollographql-client-version"]).to(equal("test-client-version")) |
| 167 | + expect(httpHeaderFields["apollographql-client-name"]).to(equal("test-client")) |
| 168 | + |
| 169 | + guard |
| 170 | + let httpBody = urlRequest.httpBody, |
| 171 | + let multipartBody = String(data: httpBody, encoding: .utf8) |
| 172 | + else { |
| 173 | + fail("Could not deserialize client library extension.") |
| 174 | + return |
| 175 | + } |
| 176 | + |
| 177 | + expect(multipartBody).to(contain("{\"extensions\":{\"clientLibrary\":{\"name\":\"apollo-ios\",\"version\":\"\(Constants.ApolloClientVersion)\"}},\"operationName\":\"MockOperationName\",\"query\":\"Mock Operation Definition\",\"variables\":{\"x\":null}}")) |
| 178 | + } |
| 179 | + |
| 180 | + func test__uploadRequest__usingInitializerDisablingClientExtension_shouldAddClientHeaders_doesNotAddClientExtension() throws { |
| 181 | + let uploadRequest = UploadRequest( |
| 182 | + graphQLEndpoint: TestURL.mockServer.url, |
| 183 | + operation: MockQuery<Hero>(), |
| 184 | + clientName: "test-client", |
| 185 | + clientVersion: "test-client-version", |
| 186 | + files: [GraphQLFile(fieldName: "x", originalName: "y", data: "z".data(using: .utf8)!)], |
| 187 | + sendEnhancedClientAwareness: false |
| 188 | + ) |
| 189 | + |
| 190 | + let urlRequest = try uploadRequest.toURLRequest() |
| 191 | + |
| 192 | + guard let httpHeaderFields = urlRequest.allHTTPHeaderFields else { |
| 193 | + fail("Missing HTTP header fields!") |
| 194 | + return |
| 195 | + } |
| 196 | + |
| 197 | + expect(httpHeaderFields["apollographql-client-version"]).to(equal("test-client-version")) |
| 198 | + expect(httpHeaderFields["apollographql-client-name"]).to(equal("test-client")) |
| 199 | + |
| 200 | + guard |
| 201 | + let httpBody = urlRequest.httpBody, |
| 202 | + let multipartBody = String(data: httpBody, encoding: .utf8) |
| 203 | + else { |
| 204 | + fail("Could not deserialize client library extension.") |
| 205 | + return |
| 206 | + } |
| 207 | + |
| 208 | + expect(multipartBody).notTo(contain("\"extensions\":{\"clientLibrary\":{\"name\":\"apollo-ios\",\"version\":\"\(Constants.ApolloClientVersion)\"}}")) |
| 209 | + } |
| 210 | +} |
0 commit comments