Skip to content

Commit

Permalink
Cookie support
Browse files Browse the repository at this point in the history
  • Loading branch information
tib committed Apr 28, 2021
1 parent d9f2467 commit 61d0382
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 6 deletions.
13 changes: 11 additions & 2 deletions Package.resolved
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,15 @@
"version": "4.2.4"
}
},
{
"package": "multipart-kit",
"repositoryURL": "https://github.com/vapor/multipart-kit.git",
"state": {
"branch": null,
"revision": "4fcc81d2ce9210a0debceb015a210c2d2eb1da71",
"version": "4.0.2"
}
},
{
"package": "routing-kit",
"repositoryURL": "https://github.com/vapor/routing-kit.git",
Expand Down Expand Up @@ -123,8 +132,8 @@
"repositoryURL": "https://github.com/vapor/vapor.git",
"state": {
"branch": null,
"revision": "5148f02e42149f73f6616452062cf96b64afbda6",
"version": "4.36.2"
"revision": "338c4be2362cac6a465bcda2ef73a189d06b18a6",
"version": "4.44.2"
}
},
{
Expand Down
24 changes: 20 additions & 4 deletions Sources/Spec/Spec.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,16 @@ public final class Spec {
private var bearerToken: String? = nil
private var headers: HTTPHeaders = [:]
private var buffer: ByteBuffer? = nil
private var beforeRequest: (inout XCTHTTPRequest) throws -> () = { _ in }
private var beforeRequests: [(inout XCTHTTPRequest) throws -> ()] = []
private var expectations: [(XCTHTTPResponse) throws -> ()] = []

private func beforeRequest() -> (inout XCTHTTPRequest) throws -> () {
{ [unowned self] req in
for item in beforeRequests {
try item(&req)
}
}
}

internal init(name: String, app: Application) {
self.name = name
Expand Down Expand Up @@ -60,11 +68,19 @@ public final class Spec {
return self
}

///set a content as the request body
public func cookie(_ cookie: HTTPCookies) -> Self {
self.beforeRequests.append({ req in
req.headers.cookie = cookie
})
return self
}

///set a content as the request body
public func body<T: Content>(_ body: T) -> Self {
self.beforeRequest = { req in
self.beforeRequests.append({ req in
try req.content.encode(body)
}
})
return self
}

Expand Down Expand Up @@ -127,7 +143,7 @@ public final class Spec {
body: self.buffer,
file: file,
line: line,
beforeRequest: self.beforeRequest,
beforeRequest: self.beforeRequest(),
afterResponse: afterRequest)
}
}

0 comments on commit 61d0382

Please sign in to comment.