Parsing Fragment #97
-
Hi all, I've been trying URLRouting particularly on the Fragment() but I need some help. I've tried the example in URLRoutingTests like this:
Seems like something is wrong with the library? By the way I'm trying to parse |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
Hi @ker0beros, Not sure if you've solved this issue or not. You are using an extension on let path = try? r.path(for: .privacyPolicy(section: "SECTION"))
↑ So in the above case the output you got would be expected ( Instead, you would want to let data = try r.print(.privacyPolicy(section: "SECTION"))
↑
let url = URLComponents(data: data).url!
print(url.absoluteString)
// prints: /legal/privacy#SECTION Parsing your URL's fragment is a little bit more complex. Below is one way to do it: func testFragment() throws {
struct Token: Equatable {
var value: String
var type: String
}
enum AppRoute: Equatable {
case auth(redirect: String, accessToken: Token)
case privacyPolicy(section: String)
}
let tokenParser = ParsePrint(.memberwise(Token.init(value:type:))) {
"access_token="
Prefix { $0 != "&" }.map(.string)
"&"
"token_type="
Prefix { $0 != "&" }.map(.string)
}
.eraseToAnyParserPrinter()
let route = Route(.case(AppRoute.auth(redirect:accessToken:))) {
Path {
"auth"
}
Query {
Field("redirect")
}
Fragment {
tokenParser
}
}
request = try XCTUnwrap(URLRequestData(string: "http://localhost:8080/auth?redirect=http://localhost:8080/#access_token=ya2OaJ169&token_type=Bearer"))
// Use `route.parse` to go from URLRequestData -> AppRoute.auth
XCTAssertEqual(
.auth(redirect: "http://localhost:8080/", accessToken: .init(value: "ya2OaJ169", type: "Bearer")),
try route.parse(&request)
)
// Use `route.print` to go from AppRoute.auth -> URLRequestData
XCTAssertEqual(
URLRequestData(/*scheme: "http", host: "localhost", port: 8080, */path: "auth", query: ["redirect": ["http://localhost:8080/"]], fragment: "access_token=ya2OaJ169&token_type=Bearer"),
try route.print(.auth(redirect: "http://localhost:8080/", accessToken: .init(value: "ya2OaJ169", type: "Bearer")))
)
} |
Beta Was this translation helpful? Give feedback.
-
Thanks a lot @robfeldmann. Really appreciate it. Hopefully this will benefit others. |
Beta Was this translation helpful? Give feedback.
Hi @ker0beros,
Not sure if you've solved this issue or not. You are using an extension on
Route
that is designed to return just thepath
component of a route:So in the above case the output you got would be expected (
/legal/privacy
).Instead, you would want to
print
the route like this:Parsing your URL's fragment is a little bit more complex. Below is one way to do it: