Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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 .github/workflows/workflow.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ jobs:
opam-pin: false

- run: |
yarn
opam pin add cohttp-async.dev . --no-action
opam pin add cohttp-lwt-jsoo.dev . --no-action
opam pin add cohttp-lwt-unix.dev . --no-action
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ _build/
*.install
.merlin
_opam/
node_modules
1 change: 1 addition & 0 deletions cohttp-lwt-jsoo.opam
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ depends: [
"cohttp" {= version}
"cohttp-lwt" {= version}
"lwt" {>= "3.0.0"}
"lwt_ppx" {with-test}
"js_of_ocaml" {>= "3.3.0"}
Comment thread
mefyl marked this conversation as resolved.
"js_of_ocaml-ppx" {>= "3.3.0"}
"js_of_ocaml-lwt" {>= "3.5.0"}
Expand Down
34 changes: 34 additions & 0 deletions cohttp_lwt_jsoo_test/src/cohttp_lwt_jsoo_test.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
module Client = Cohttp_lwt_jsoo.Client
module Js = Js_of_ocaml.Js

let _Promise = Js.Unsafe.global##._Promise
let ( let* ) = Lwt.( >>= )
let ( let+ ) = Lwt.( >|= )

let promise_of_lwt lwt =
new%js _Promise
(Js.wrap_callback (fun resolve reject ->
try%lwt
let+ res = lwt () in
Js.Unsafe.fun_call resolve [| Js.Unsafe.inject res |]
with e ->
let msg = Printexc.to_string e in
Js.Unsafe.fun_call reject
[| Js.Unsafe.inject (new%js Js.error_constr (Js.string msg)) |]))

let () =
Js.export_all
(object%js
method request uri =
let f () =
let uri = Uri.of_string (Js.to_string uri) in
let* response, body = Client.get uri in
let+ body = Cohttp_lwt.Body.to_string body in
let status =
Cohttp.Response.status response |> Cohttp.Code.code_of_status
in
Js.array
[| Js.Unsafe.inject status; Js.Unsafe.inject @@ Js.string body |]
in
promise_of_lwt f
end)
12 changes: 12 additions & 0 deletions cohttp_lwt_jsoo_test/src/dune
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
(executable
(name cohttp_lwt_jsoo_test)
(modes js)
(libraries cohttp cohttp-lwt cohttp-lwt-jsoo lwt js_of_ocaml-lwt)
(preprocess
(pps lwt_ppx js_of_ocaml-ppx)))

(rule
(alias runtest)
(deps test.js cohttp_lwt_jsoo_test.bc.js)
(action
(run ./test.js)))
19 changes: 19 additions & 0 deletions cohttp_lwt_jsoo_test/src/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#!/usr/bin/env node

const assert = require('assert');

global.XMLHttpRequest = require("xmlhttprequest").XMLHttpRequest;
const tests = require('./cohttp_lwt_jsoo_test.bc.js')

async function main() {
{
const [status, body] = await tests.request("https://mirage.io");
Comment thread
mseri marked this conversation as resolved.
assert(status == 200);
}
{
const [status, body] = await tests.request("https://this.domain.does.not.exist");
assert(status == 0);

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test checks what e18e6a8 fixed.

}
}

main()
10 changes: 10 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"name": "ocaml-cohttp",
"version": "4.0.0",
"description": "An OCaml library for HTTP clients and servers",
"repository": "https://github.com/mirage/ocaml-cohttp",
"license": "ISC",
"dependencies": {
"xmlhttprequest": "^1.8.0"
}
}
8 changes: 8 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
# yarn lockfile v1


xmlhttprequest@^1.8.0:
version "1.8.0"
resolved "https://registry.yarnpkg.com/xmlhttprequest/-/xmlhttprequest-1.8.0.tgz#67fe075c5c24fef39f9d65f5f7b7fe75171968fc"
integrity sha1-Z/4HXFwk/vOfnWX197f+dRcZaPw=