Skip to content

Commit

Permalink
update Mock to handle errors
Browse files Browse the repository at this point in the history
  • Loading branch information
fumito-ito committed Oct 24, 2024
1 parent fcf5db3 commit f09c437
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 3 deletions.
18 changes: 16 additions & 2 deletions Sources/AnthropicSwiftSDK-TestUtils/HTTPMock.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ public enum MockInspectType {
case request((URLRequest) -> Void, String?)
case requestHeader(([String: String]?) -> Void, String?)
case response(String)
case error(String)
}

public class HTTPMock: URLProtocol {
Expand All @@ -35,24 +36,37 @@ public class HTTPMock: URLProtocol {
}

if let url = request.url,
let response = HTTPURLResponse(url: url, statusCode: 200, httpVersion: "HTTP/2", headerFields: nil) {
client?.urlProtocol(self, didReceive: response, cacheStoragePolicy: .notAllowed)
let succeedResponse = HTTPURLResponse(url: url, statusCode: 200, httpVersion: "HTTP/2", headerFields: nil),
let errorResponse = HTTPURLResponse(url: url, statusCode: 400, httpVersion: "HTTP/2", headerFields: nil) {

switch Self.inspectType {
case .none:
client?.urlProtocol(self, didReceive: succeedResponse, cacheStoragePolicy: .notAllowed)
client?.urlProtocol(self, didLoad: getBasicJSONStringData())
case .request(_, nil), .requestHeader(_, nil):
client?.urlProtocol(self, didReceive: succeedResponse, cacheStoragePolicy: .notAllowed)
case .request(_, let jsonString), .requestHeader(_, let jsonString):
client?.urlProtocol(self, didReceive: succeedResponse, cacheStoragePolicy: .notAllowed)
guard let jsonString, let data = jsonString.data(using: .utf8) else {
client?.urlProtocol(self, didLoad: getBasicJSONStringData())
return
}
client?.urlProtocol(self, didLoad: data)
case .response(let jsonString):
client?.urlProtocol(self, didReceive: succeedResponse, cacheStoragePolicy: .notAllowed)
guard let data = jsonString.data(using: .utf8) else {
client?.urlProtocol(self, didLoad: getBasicJSONStringData())
return
}
client?.urlProtocol(self, didLoad: data)
case .error(let responseJSON):
client?.urlProtocol(self, didReceive: errorResponse, cacheStoragePolicy: .notAllowed)
guard
let data = responseJSON.data(using: .utf8) else {
client?.urlProtocol(self, didLoad: getBasicJSONStringData())
return
}
client?.urlProtocol(self, didLoad: data)
}
}

Expand Down
21 changes: 20 additions & 1 deletion Tests/AnthropicSwiftSDKTests/MessagesTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,26 @@ final class MessagesTests: XCTestCase {
HTTPMock.inspectType = .requestHeader({ headers in
XCTAssertEqual(headers!["x-api-key"], "This-is-test-API-key")
expectation.fulfill()
}, nil)
}, """
{
"id": "msg_01XFDUDYJgAACzvnptvVoYEL",
"type": "message",
"role": "assistant",
"content": [
{
"type": "text",
"text": "Hello!"
}
],
"model": "claude-2.1",
"stop_reason": "end_turn",
"stop_sequence": null,
"usage": {
"input_tokens": 12,
"output_tokens": 6
}
}
""")

let message = Message(role: .user, content: [.text("This is test text")])
let _ = try await messages.createMessage([message], maxTokens: 1024)
Expand Down

0 comments on commit f09c437

Please sign in to comment.