Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
mbrandonw committed Sep 10, 2020
1 parent f9f24c1 commit a9392c0
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 11 deletions.
12 changes: 5 additions & 7 deletions Sources/ApplicativeRouter/Combinators.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public func lit(_ str: String) -> Router<Void> {
uncons(route.path)
.flatMap { p, ps in
p == str
? (.init(method: route.method, path: ps, query: route.query, body: route.body), ())
? (.init(method: route.method, path: ps, query: route.query, body: route.body, headers: route.headers), ())
: nil
}
},
Expand All @@ -25,7 +25,7 @@ public func pathParam<A>(_ f: PartialIso<String, A>) -> Router<A> {
return Router<A>(
parse: { route in
guard let (p, ps) = uncons(route.path), let v = f.apply(p) else { return nil }
return (RequestData(method: route.method, path: ps, query: route.query, body: route.body), v)
return (RequestData(method: route.method, path: ps, query: route.query, body: route.body, headers: route.headers), v)
},
print: { a in
.init(method: nil, path: [f.unapply(a) ?? ""], query: [], body: nil)
Expand Down Expand Up @@ -59,12 +59,10 @@ public func queryParam<A>(_ key: String, _ f: PartialIso<String?, A>) -> Router<
})
}

public func header<A>(_ key: String, _ f: PartialIso<String?, A>) -> Router<A> {
public func header<A>(_ key: String, _ f: PartialIso<String, A>) -> Router<A> {
.init(
parse: { (request: RequestData) in
request.headers
.first(where: { k, _ in k == key })
.map(\.value)
request.headers[key]
.flatMap(f.apply)
.map { (request, $0) }
},
Expand All @@ -74,7 +72,7 @@ public func header<A>(_ key: String, _ f: PartialIso<String?, A>) -> Router<A> {
path: [],
query: [],
body: nil,
headers: f.unapply(value).map { [(key, $0)] } ?? []
headers: f.unapply(value).map { [key: $0] } ?? [:]
)
},
template: { _ in fatalError() }
Expand Down
4 changes: 2 additions & 2 deletions Sources/ApplicativeRouter/RequestData.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ struct RequestData: Monoid {
var path: [String] = []
var query: [(key: String, value: String?)] = []
var body: Data? = nil
var headers: [(key: String, value: String?)] = []
var headers: [String: String] = [:]

static var empty = RequestData()

Expand All @@ -17,7 +17,7 @@ struct RequestData: Monoid {
query: lhs.query + rhs.query,
// todo: is coalescing enough or should we be appending?
body: lhs.body ?? rhs.body,
headers: lhs.headers + rhs.headers
headers: lhs.headers.merging(rhs.headers, uniquingKeysWith: { $1 })
)
}
}
16 changes: 14 additions & 2 deletions Sources/ApplicativeRouter/SyntaxRouter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -167,15 +167,27 @@ private func requestData(from request: URLRequest) -> RequestData {
let method = request.httpMethod.flatMap(Method.init(string:)) ?? .get

guard let url = request.url else {
return .init(method: method, path: [], query: [], body: request.httpBody)
return .init(
method: method,
path: [],
query: [],
body: request.httpBody,
headers: request.allHTTPHeaderFields ?? [:]
)
}

let query = parse(query: url.query ?? "")

let path = url.path.components(separatedBy: "/")
|> mapOptional { $0.isEmpty ? nil : $0 }

return .init(method: method, path: path, query: query, body: request.httpBody)
return .init(
method: method,
path: path,
query: query,
body: request.httpBody,
headers: request.allHTTPHeaderFields ?? [:]
)
}

private func request(from data: RequestData) -> URLRequest? {
Expand Down
11 changes: 11 additions & 0 deletions Tests/ApplicativeRouterTests/SyntaxRouterTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -206,4 +206,15 @@ class SyntaxRouterTests: XCTestCase {
router.absoluteString(for: .redirect("http://localhost:8080/home?redirect=http://localhost:8080/home"))
)
}

func testHeader() {
let router: Router<Int> = get %> "home" %> header("version", .int) <% end

var request = URLRequest(url: URL(string: "home")!)
request.allHTTPHeaderFields = ["version": "10"]
XCTAssertEqual(
router.match(request: request),
10
)
}
}

0 comments on commit a9392c0

Please sign in to comment.