Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds protocol support to make-domain-requester #22

Merged
merged 4 commits into from
Aug 26, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,6 @@ install:

script:
- raco test -c request
- raco test --submodule integration-test request
- raco doc-coverage request
- if [ -n "$RUN_COVER" ]; then raco cover -f coveralls -d $TRAVIS_BUILD_DIR/coverage -c request; fi
43 changes: 40 additions & 3 deletions request/private/http-location.rkt
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,12 @@

(provide
(contract-out
[make-domain-requester (-> string? requester? requester?)]
[make-host+port-requester (-> string? exact-nonnegative-integer? requester? requester?)]))
[make-domain-requester
(-> string? requester? requester?)]
[make-host+port-requester
(-> string? exact-nonnegative-integer? requester? requester?)]
[make-https-requester
(-> requester? requester?)]))


(define (domain+relative-path->http-url domain relative-path)
Expand All @@ -17,8 +21,41 @@
(define (host+port->domain host port)
(format "~a:~a" host port))

(define (http-url->https-url location)
(struct-copy url location [scheme "https"]))

(define (make-domain-requester domain requester)
(wrap-requester-location (domain+relative-path->http-url domain _) requester))
(wrap-requester-location
(domain+relative-path->http-url domain _) requester))

(define (make-https-requester requester)
(wrap-requester-location
(http-url->https-url _) requester))

(define (make-host+port-requester host port requester)
(make-domain-requester (host+port->domain host port) requester))

(module+ integration-test
(require json
rackunit
"base.rkt"
"call-response.rkt")

(define domain "httpbin.org")
(define http-url (domain+relative-path->http-url domain "/"))
(define http-req (make-domain-requester domain http-requester))
(define https-req (make-domain-requester
domain (make-https-requester http-requester)))

(define http-resp (get http-req "/get"))
(define https-resp (get https-req "/get"))

(check-pred url? http-url)
(check-equal? (url-scheme http-url) "http")
(check-equal?
(hash-ref (string->jsexpr (http-response-body https-resp)) 'url)
"https://httpbin.org/get")

(check-pred requester? http-req)
(check-equal? (http-response-code http-resp) 200)
(check-equal? (http-response-code https-resp) 200))
17 changes: 15 additions & 2 deletions request/private/http-location.scrbl
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,11 @@ relative paths as locations.
@racket[domain] to construct a full http @racket[url],
which is then passed to the underlying @racket[requester].
The relative path should not begin with a slash.
@racketinput[
@racketblock[
(define foo-com-requester
(make-domain-requester "foo.com" http-requester))
(get foo-com-requester "some/sort/of/path") ;; request to http://foo.com/some/sort/of/path
(code:comment @#,elem{request to http://foo.com/some/sort/of/path})
(get foo-com-requester "some/sort/of/path")
]}

@defproc[(make-host+port-requester [host string?]
Expand All @@ -39,3 +40,15 @@ relative paths as locations.
@racket[(make-host+port-requester "foo.com" 8080 some-requester)]
is equivalent to @racket[(make-domain-requester "foo.com:8080" some-requester)]
}

@defproc[(make-https-requester [requester requester?])
requester?]{
Given a requester that accepts @racket[url?]s
as locations, returns a requester that accepts a @racket[url] and converts
it to use an https scheme before being passed to the underlying @racket[requester].
@racketblock[
(define foo-https-requester
(make-domain-requester "foo.com" (make-https-requester http-requester)))
(code:comment @#,elem{request to https://foo.com/some/sort/of/path})
(get foo-https-requester "some/sort/of/path")
]}