-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding api key header to v3 profile request (#257)
* Ignoring api-key file * Adding ApiKey config to ProfileService headers * Adding ApiKey to demo app * Improve script to create secrets file * Generate secrets file from make command * Try build demo in CI * Using `Configuration.shared.apiKey` as default on URLRequest.authorised * Update Makefile * Making Configuration an actor * Update openapi specs * Adding profile service tests
- Loading branch information
Showing
15 changed files
with
187 additions
and
374 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
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 |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import Foundation | ||
|
||
public actor Configuration { | ||
private(set) var apiKey: String? | ||
public static let shared = Configuration() | ||
|
||
private init() {} | ||
|
||
public func configure(with apiKey: String?) { | ||
self.apiKey = apiKey | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,54 @@ | ||
@testable import Gravatar | ||
import XCTest | ||
|
||
final class ProfileServiceTests: XCTestCase { | ||
override func tearDown() async throws { | ||
await Configuration.shared.configure(with: nil) | ||
} | ||
|
||
func testProfileRequest() async { | ||
guard let data = Bundle.fullProfileJsonData else { | ||
return XCTFail("Could not create data") | ||
} | ||
let session = URLSessionMock(returnData: data, response: .successResponse()) | ||
let service = ProfileService(client: HTTPClientMock(session: session)) | ||
|
||
do { | ||
_ = try await service.fetch(with: .hashID("")) | ||
XCTAssertNil(session.request?.value(forHTTPHeaderField: "Authorization")) | ||
} catch { | ||
XCTFail(error.localizedDescription) | ||
} | ||
} | ||
|
||
func testProfileRequestWithApiKey() async { | ||
guard let data = Bundle.fullProfileJsonData else { | ||
return XCTFail("Could not create data") | ||
} | ||
|
||
await Configuration.shared.configure(with: "somekey") | ||
|
||
let session = URLSessionMock(returnData: data, response: .successResponse()) | ||
let service = ProfileService(client: HTTPClientMock(session: session)) | ||
|
||
do { | ||
_ = try await service.fetch(with: .hashID("")) | ||
XCTAssertNotNil(session.request?.value(forHTTPHeaderField: "Authorization")) | ||
} catch { | ||
XCTFail(error.localizedDescription) | ||
} | ||
} | ||
} | ||
|
||
extension Bundle { | ||
func jsonData(forResource resource: String) -> Data? { | ||
guard let url = Bundle.testsBundle.url(forResource: resource, withExtension: "json") else { | ||
return nil | ||
} | ||
return try? Data(contentsOf: url) | ||
} | ||
|
||
static var fullProfileJsonData: Data? { | ||
testsBundle.jsonData(forResource: "fullProfile") | ||
} | ||
} |
Oops, something went wrong.