Skip to content

Commit

Permalink
Add integration test for AccessTokenPlugin with stubs
Browse files Browse the repository at this point in the history
  • Loading branch information
sunshinejr committed Sep 17, 2019
1 parent 9862538 commit 3e1d328
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 4 deletions.
28 changes: 28 additions & 0 deletions Tests/MoyaTests/MoyaProviderIntegrationTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,34 @@ final class MoyaProviderIntegrationTests: QuickSpec {
expect(log).to(contain("{ URL: https://api.github.com/zen }"))
}
}

describe("a provider with AccessTokenPlugin") {
var token = ""
var plugin: AccessTokenPlugin!
var provider: MoyaProvider<HTTPBin>!

beforeEach {
token = UUID().uuidString
plugin = AccessTokenPlugin { token }
provider = MoyaProvider<HTTPBin>(stubClosure: MoyaProvider.immediatelyStub,
plugins: [plugin])
}

it("correctly modifies authorization header field") {
var header: String?

waitUntil { done in
provider.request(.bearer) { result in
if case .success(let response) = result {
header = response.request?.value(forHTTPHeaderField: "Authorization")
}
done()
}
}

expect(header).to(equal("Bearer \(token)"))
}
}
}

describe("a reactive provider with SignalProducer") {
Expand Down
22 changes: 18 additions & 4 deletions Tests/MoyaTests/TestHelpers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,9 @@ let failureEndpointClosure = { (target: GitHub) -> Endpoint in
return Endpoint(url: url(target), sampleResponseClosure: {.networkError(error)}, method: target.method, task: target.task, httpHeaderFields: target.headers)
}

enum HTTPBin: TargetType {
enum HTTPBin: TargetType, AccessTokenAuthorizable {
case basicAuth
case bearer
case post
case upload(file: URL)
case uploadMultipart([MultipartFormData], [String: Any]?)
Expand All @@ -81,14 +82,16 @@ enum HTTPBin: TargetType {
switch self {
case .basicAuth:
return "/basic-auth/user/passwd"
case .bearer:
return "/bearer"
case .post, .upload, .uploadMultipart, .validatedUploadMultipart:
return "/post"
}
}

var method: Moya.Method {
switch self {
case .basicAuth:
case .basicAuth, .bearer:
return .get
case .post, .upload, .uploadMultipart, .validatedUploadMultipart:
return .post
Expand All @@ -97,8 +100,8 @@ enum HTTPBin: TargetType {

var task: Task {
switch self {
case .basicAuth, .post:
return .requestParameters(parameters: [:], encoding: URLEncoding.default)
case .basicAuth, .post, .bearer:
return .requestParameters(parameters: [:], encoding: URLEncoding.default)
case .upload(let fileURL):
return .uploadFile(fileURL)
case .uploadMultipart(let data, let urlParameters), .validatedUploadMultipart(let data, let urlParameters, _):
Expand All @@ -114,6 +117,8 @@ enum HTTPBin: TargetType {
switch self {
case .basicAuth:
return "{\"authenticated\": true, \"user\": \"user\"}".data(using: String.Encoding.utf8)!
case .bearer:
return "{\"authenticated\": true, \"token\": \"4D4A9C7D-F6E7-4FD7-BDBD-03880550A80D\"}".data(using: String.Encoding.utf8)!
case .post, .upload, .uploadMultipart, .validatedUploadMultipart:
return "{\"args\": {}, \"data\": \"\", \"files\": {}, \"form\": {}, \"headers\": { \"Connection\": \"close\", \"Content-Length\": \"0\", \"Host\": \"httpbin.org\" }, \"json\": null, \"origin\": \"198.168.1.1\", \"url\": \"https://httpbin.org/post\"}".data(using: String.Encoding.utf8)!
}
Expand All @@ -131,6 +136,15 @@ enum HTTPBin: TargetType {
return .none
}
}

var authorizationType: AuthorizationType {
switch self {
case .bearer:
return .bearer
default:
return .none
}
}
}

public enum GitHubUserContent {
Expand Down

0 comments on commit 3e1d328

Please sign in to comment.