Skip to content

Commit

Permalink
basic test http server
Browse files Browse the repository at this point in the history
  • Loading branch information
rfaulhaber committed Oct 23, 2024
1 parent cce9462 commit 85c866b
Show file tree
Hide file tree
Showing 7 changed files with 141 additions and 47 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ jobs:
- name: Compile module
run: |
just build
- name: install pueue for tests
run: |
cargo install --locked pueue
- name: Run tests
run: |
just test
109 changes: 109 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@
nodePackages_latest.eask

just
pueue
];
in
forAllSystems
Expand Down
2 changes: 2 additions & 0 deletions http_server/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,5 @@ http-body-util = "0.1.2"
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
tokio = { version = "1.40.0", features = ["macros", "rt-multi-thread"] }
tracing = "0.1.40"
tracing-subscriber = "0.3.18"
15 changes: 11 additions & 4 deletions http_server/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,18 @@ struct Response<'r> {
async fn main() {
let app = Router::new()
.route("/anything", any(anything))
.route("/anything/*rest", any(anything))
.route("/status", any(status));

let listener = tokio::net::TcpListener::bind("0.0.0.0:8080").await.unwrap();
axum::serve(listener, app).await.unwrap();
}

async fn anything(request: Request) -> String {
let headers = extract_headers(request.headers());

let (parts, body) = request.into_parts();

let headers = extract_headers(&parts.headers);

let query = parts.uri.query();

let query_parameters = query.map(extract_query_parameters);
Expand Down Expand Up @@ -75,6 +76,12 @@ fn extract_query_parameters<'p>(query: &'p str) -> QueryParameters<'p> {
map
}

fn extract_headers<'h>(headers: &HeaderMap<HeaderValue>) -> HashMap<&'h str, &'h str> {
todo!();
fn extract_headers<'h>(headers: &'h HeaderMap<HeaderValue>) -> HashMap<&'h str, &'h str> {
let mut map = HashMap::new();

for header in headers.keys() {
map.insert(header.as_str(), headers.get(header).unwrap().to_str().unwrap());
}

map
}
34 changes: 5 additions & 29 deletions justfile
Original file line number Diff line number Diff line change
Expand Up @@ -6,39 +6,15 @@ build env="dev":
eask install

test: (build "dev")
-eask test buttercup

test-full: (build "dev")
just test-setup
-eask test buttercup
just test-teardown
cargo build -p http_server
pueued -d
pueue add "cargo run -p http_server"
eask test buttercup
pueue kill --all

dist: (build "release")
eask package

clean:
cargo clean
rm libreel.*

[private]
test-setup:
#!/usr/bin/env nu
print "preparing test environment"
^docker run -d --rm --name reel-test -p 8080:80 kennethreitz/httpbin

mut state = (^docker inspect reel-test | from json | first | get State.status)

print $"state: ($state)"

while $state != "running" {
print "waiting..."
$state = (^docker inspect reel-test | from json | first | get State.status)
}

sleep 1sec

[private]
test-teardown:
#!/usr/bin/env nu
print "tearing down"
^docker stop reel-test
24 changes: 10 additions & 14 deletions tests/test-reel.el
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,23 @@
(require 'buttercup)
(require 'reel)

;; NOTE these tests expect a local instance of httpbin to be running
;; run:
;; docker run -it --rm --name reel-test -p 8080:80 kennethreitz/httpbin
;; to have these tests run properly!

(defconst reel-test-endpoint "http://127.0.0.1:8080/anything")
(defconst reel-test-endpoint "http://127.0.0.1:8080")
(defconst reel-test-container-name "reel-test")

(describe "reel"
(describe "reel fn"
(it "makes basic calls with default arguments"
(let* ((url (format "%s/foo/bar" reel-test-endpoint))
(let* ((path "/anything/foo/bar")
(url (concat reel-test-endpoint path))
(result (reel url)))
(expect result :to-be-truthy)
(with-slots (status body) result
(expect status :to-equal 200)
(let ((json-body (json-parse-string body)))
(expect (gethash "url" json-body) :to-equal url)
(expect (gethash "url" json-body) :to-equal path)
(expect (gethash "method" json-body) :to-equal "GET")))))
(it "makes basic POST calls"
(let* ((url (concat reel-test-endpoint "/resource"))
(let* ((url (concat reel-test-endpoint "/anything"))
(result
(reel url
:method "POST"
Expand All @@ -34,10 +30,10 @@
(with-slots (status body) result
(expect status :to-equal 200)
(let ((json-body (json-parse-string body)))
(expect (gethash "data" json-body) :to-equal "hello world")
(expect (gethash "body" json-body) :to-equal "hello world")
(expect (gethash "method" json-body) :to-equal "POST")))))
(it "makes basic calls with headers"
(let* ((url (concat reel-test-endpoint "/resource"))
(let* ((url (concat reel-test-endpoint "/anything"))
(result
(reel url
:headers '(("header-1" . "value")))))
Expand All @@ -47,9 +43,9 @@
(let ((json-body (json-parse-string body)))
(expect (gethash "method" json-body) :to-equal "GET")
(let ((headers (gethash "headers" json-body)))
(expect (gethash "Header-1" headers) :to-equal "value"))))))
(it "makes basic calls with form data"
(let* ((url (concat reel-test-endpoint "/resource"))
(expect (gethash "header-1" headers) :to-equal "value"))))))
(xit "makes basic calls with form data"
(let* ((url (concat reel-test-endpoint "/anything"))
(result
(reel url
:method "POST"
Expand Down

0 comments on commit 85c866b

Please sign in to comment.