-
Notifications
You must be signed in to change notification settings - Fork 295
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
Pass Mutation Name as Endpoint for GraphQL #1349
Changes from 5 commits
f031476
966375d
091c08f
07e8705
df95e39
ed42909
93d5bc5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -749,6 +749,36 @@ final class BTHTTP_Tests: XCTestCase { | |
} | ||
} | ||
} | ||
|
||
func testURLSessionTaskDidFinishCollectingMetrics() { | ||
let mockDelegate = MockNetworkTimingDelegate() | ||
http?.networkTimingDelegate = mockDelegate | ||
|
||
var originalRequest = URLRequest(url: URL(string: "https://example.com/graphql")!) | ||
originalRequest.httpBody = """ | ||
{ | ||
"operationName": "TestMutation" | ||
} | ||
""".data(using: .utf8) | ||
let task = testURLSession.dataTask(with: originalRequest) | ||
|
||
let transactionMetrics = MockURLSessionTaskTransactionMetrics() | ||
transactionMetrics.mockConnectStartDate = Date() | ||
transactionMetrics.mockFetchStartDate = Date() | ||
Comment on lines
+766
to
+767
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. just curious, how are these two values different? 👀 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Apple has an amazing graphic in their documentation that would explain it better. Apple doc reference |
||
transactionMetrics.mockResponseEndDate = Date().addingTimeInterval(1) | ||
transactionMetrics.mockRequest = URLRequest(url: URL(string: "https://example.com/graphql")!) | ||
|
||
let metrics = MockURLSessionTaskMetrics(transactionMetrics: [transactionMetrics]) | ||
|
||
http?.urlSession(testURLSession, task: task, didFinishCollecting: metrics) | ||
|
||
XCTAssertTrue(mockDelegate.didCallFetchAPITiming) | ||
XCTAssertEqual(mockDelegate.receivedPath, "mutation TestMutation") | ||
XCTAssertNotNil(mockDelegate.receivedConnectionStartTime) | ||
XCTAssertNotNil(mockDelegate.receivedRequestStartTime) | ||
XCTAssertNotNil(mockDelegate.receivedStartTime) | ||
XCTAssertNotNil(mockDelegate.receivedEndTime) | ||
} | ||
|
||
// MARK: - Helper Methods | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
@testable import BraintreeCore | ||
import Foundation | ||
|
||
class MockNetworkTimingDelegate: BTHTTPNetworkTiming { | ||
var didCallFetchAPITiming = false | ||
var receivedPath: String? | ||
var receivedConnectionStartTime: Int? | ||
var receivedRequestStartTime: Int? | ||
var receivedStartTime: Int? | ||
var receivedEndTime: Int? | ||
|
||
func fetchAPITiming(path: String, connectionStartTime: Int?, requestStartTime: Int?, startTime: Int, endTime: Int) { | ||
didCallFetchAPITiming = true | ||
receivedPath = path | ||
receivedConnectionStartTime = connectionStartTime | ||
receivedRequestStartTime = requestStartTime | ||
receivedStartTime = startTime | ||
receivedEndTime = endTime | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import Foundation | ||
|
||
class MockURLSessionTaskTransactionMetrics: URLSessionTaskTransactionMetrics { | ||
|
||
var mockConnectStartDate: Date? | ||
var mockFetchStartDate: Date? | ||
var mockResponseEndDate: Date? | ||
var mockRequest: URLRequest? | ||
|
||
override var connectStartDate: Date? { | ||
mockConnectStartDate | ||
} | ||
|
||
override var fetchStartDate: Date? { | ||
mockFetchStartDate | ||
} | ||
|
||
override var responseEndDate: Date? { | ||
mockResponseEndDate | ||
} | ||
|
||
override var request: URLRequest { | ||
mockRequest ?? URLRequest(url: URL(string: "https://example.com")!) | ||
} | ||
} | ||
|
||
class MockURLSessionTaskMetrics: URLSessionTaskMetrics { | ||
var mockTransactionMetrics: [URLSessionTaskTransactionMetrics] | ||
|
||
/// For testing only | ||
init(transactionMetrics: [URLSessionTaskTransactionMetrics]) { | ||
self.mockTransactionMetrics = transactionMetrics | ||
} | ||
|
||
override var transactionMetrics: [URLSessionTaskTransactionMetrics] { | ||
mockTransactionMetrics | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Are we able to add a unit test for this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure, added: df95e39