This repository has been archived by the owner on Apr 20, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
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 #62 from nodes-vapor/feature/filter-headers
Add tests for the keyFilter feature & apply filter everywhere
- Loading branch information
Showing
5 changed files
with
210 additions
and
7 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -55,4 +55,172 @@ final class BugsnagTests: XCTestCase { | |
let request = Request(using: application) | ||
try reporter.report(NotFound(), on: request).wait() | ||
} | ||
|
||
func testKeyFiltersWorkInRequestBody() throws { | ||
var capturedSendReportParameters: ( | ||
host: String, | ||
headers: HTTPHeaders, | ||
body: Data, | ||
container: Container | ||
)? | ||
|
||
let reporter = BugsnagReporter( | ||
config: .init(apiKey: "apiKey", releaseStage: "test", keyFilters: ["password", "email"]), | ||
sendReport: { host, headers, data, container in | ||
capturedSendReportParameters = (host, headers, data, container) | ||
return container.future(Response(http: HTTPResponse(status: .ok), using: container)) | ||
}) | ||
let application = try Application.test() | ||
let request = Request(using: application) | ||
request.http.method = .POST | ||
request.http.body = TestBody.default.httpBody | ||
|
||
_ = try! reporter.report(NotFound(), on: request).wait() | ||
|
||
guard let params = capturedSendReportParameters else { | ||
XCTFail() | ||
return | ||
} | ||
|
||
let responseBody = try JSONDecoder().decode(BugsnagResponseBody<TestBody>.self, from: params.body) | ||
|
||
guard let body = responseBody.events.first?.request?.body else { | ||
XCTFail("Unable to parse request body") | ||
return | ||
} | ||
XCTAssertNil(body.password, "test that password is removed") | ||
XCTAssertNil(body.email, "test that email is removed") | ||
XCTAssertEqual(body.hash, TestBody.default.hash, "test that hash is not altered") | ||
} | ||
|
||
func testKeyFiltersWorkInHeaderFields() throws { | ||
var capturedSendReportParameters: ( | ||
host: String, | ||
headers: HTTPHeaders, | ||
body: Data, | ||
container: Container | ||
)? | ||
|
||
let reporter = BugsnagReporter( | ||
config: .init(apiKey: "apiKey", releaseStage: "test", keyFilters: ["password", "email"]), | ||
sendReport: { host, headers, data, container in | ||
capturedSendReportParameters = (host, headers, data, container) | ||
return container.future(Response(http: HTTPResponse(status: .ok), using: container)) | ||
}) | ||
let application = try Application.test() | ||
let request = Request(using: application) | ||
request.http.method = .POST | ||
request.http.body = TestBody.default.httpBody | ||
var headers = request.http.headers | ||
headers.add(name: HTTPHeaderName("password"), value: TestBody.default.password!) | ||
headers.add(name: HTTPHeaderName("email"), value: TestBody.default.email!) | ||
headers.add(name: HTTPHeaderName("hash"), value: TestBody.default.hash!) | ||
request.http.headers = headers | ||
|
||
_ = try! reporter.report(NotFound(), on: request).wait() | ||
|
||
guard let params = capturedSendReportParameters else { | ||
XCTFail() | ||
return | ||
} | ||
|
||
let responseBody = try JSONDecoder().decode(BugsnagResponseBody<TestBody>.self, from: params.body) | ||
|
||
guard let responseHeaders = responseBody.events.first?.request?.headers else { | ||
XCTFail("Unable to parse response headers") | ||
return | ||
} | ||
|
||
XCTAssertNil(responseHeaders["password"], "test that password is removed") | ||
XCTAssertNil(responseHeaders["email"], "test that email is removed") | ||
XCTAssertEqual(responseHeaders["hash"], TestBody.default.hash!, "test that hash is not altered") | ||
} | ||
|
||
func testKeyFiltersWorkInURLQueryParams() throws { | ||
var capturedSendReportParameters: ( | ||
host: String, | ||
headers: HTTPHeaders, | ||
body: Data, | ||
container: Container | ||
)? | ||
|
||
let reporter = BugsnagReporter( | ||
config: .init(apiKey: "apiKey", releaseStage: "test", keyFilters: ["password", "email"]), | ||
sendReport: { host, headers, data, container in | ||
capturedSendReportParameters = (host, headers, data, container) | ||
return container.future(Response(http: HTTPResponse(status: .ok), using: container)) | ||
}) | ||
let application = try Application.test() | ||
let request = Request(using: application) | ||
request.http.url = URL(string: "http://foo.bar.com/?password=\(TestBody.default.password!)&email=\(TestBody.default.email!)&hash=\(TestBody.default.hash!)")! | ||
request.http.method = .POST | ||
request.http.body = TestBody.default.httpBody | ||
var headers = request.http.headers | ||
headers.add(name: HTTPHeaderName("password"), value: TestBody.default.password!) | ||
headers.add(name: HTTPHeaderName("email"), value: TestBody.default.email!) | ||
headers.add(name: HTTPHeaderName("hash"), value: TestBody.default.hash!) | ||
request.http.headers = headers | ||
|
||
_ = try! reporter.report(NotFound(), on: request).wait() | ||
|
||
guard let params = capturedSendReportParameters else { | ||
XCTFail() | ||
return | ||
} | ||
|
||
let responseBody = try JSONDecoder().decode(BugsnagResponseBody<TestBody>.self, from: params.body) | ||
|
||
guard let responseURLString = responseBody.events.first?.request?.url else { | ||
XCTFail("Unable to parse response url") | ||
return | ||
} | ||
|
||
let urlComponents = URLComponents(string: responseURLString) | ||
let passwordItem = urlComponents?.queryItems?.filter { $0.name == "password" }.last | ||
let emailItem = urlComponents?.queryItems?.filter { $0.name == "email" }.last | ||
let hashItem = urlComponents?.queryItems?.filter { $0.name == "hash" }.last | ||
|
||
XCTAssertNil(passwordItem, "test that password is removed") | ||
XCTAssertNil(emailItem, "test that email is removed") | ||
XCTAssertEqual(hashItem?.value, TestBody.default.hash!, "test that hash is not altered") | ||
} | ||
} | ||
|
||
struct TestBody: Codable { | ||
var password: String? | ||
var email: String? | ||
var hash: String? | ||
|
||
static var `default`: TestBody { | ||
return .init(password: "TopSecret", email: "[email protected]", hash: "myAwesomeHash") | ||
} | ||
|
||
var httpBody: HTTPBody { | ||
return try! HTTPBody(data: JSONEncoder().encode(self)) | ||
} | ||
} | ||
|
||
struct BugsnagResponseBody<T: Codable>: Codable { | ||
struct Event: Codable { | ||
struct Request: Codable { | ||
let body: T? | ||
let headers: [String: String]? | ||
let url: String? | ||
|
||
// custom decoding needed as the format is JSON string (not JSON object) | ||
init(from decoder: Decoder) throws { | ||
let container = try decoder.container(keyedBy: CodingKeys.self) | ||
let bodyString = try container.decode(String.self, forKey: .body) | ||
guard let data = bodyString.data(using: .utf8) else { | ||
throw Abort(.internalServerError) | ||
} | ||
body = try JSONDecoder().decode(T.self, from: data) | ||
headers = try container.decode(Dictionary.self, forKey: .headers) | ||
url = try container.decode(String.self, forKey: .url) | ||
} | ||
} | ||
let request: Request? | ||
} | ||
let apiKey: String | ||
let events: [Event] | ||
} |
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