Skip to content

Commit 6200206

Browse files
committed
Merge pull request Kong#61 from SGrondin/master
Updated OCaml with the new Cohttp 0.16 features
2 parents c242698 + d962a36 commit 6200206

10 files changed

Lines changed: 22 additions & 34 deletions

File tree

src/targets/ocaml/cohttp.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,16 @@ module.exports = function (source, options) {
3030
// Add headers, including the cookies
3131
var headers = Object.keys(source.allHeaders)
3232

33-
if (headers.length) {
34-
code.push('let headers = Header.init ()')
33+
if (headers.length === 1) {
34+
code.push('let headers = Header.add (Header.init ()) "%s" "%s" in', headers[0], source.allHeaders[headers[0]])
35+
} else if (headers.length > 1) {
36+
code.push('let headers = Header.add_list (Header.init ()) [')
3537

3638
headers.map(function (key) {
37-
code.push(1, '|> fun h -> Header.add h "%s" "%s"', key, source.allHeaders[key])
39+
code.push(1, '("%s", "%s");', key, source.allHeaders[key])
3840
})
3941

40-
code.push('in')
42+
code.push('] in')
4143
}
4244

4345
// Add body

test/fixtures/output/ocaml/cohttp/application-form-encoded.ml

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,7 @@ open Cohttp
33
open Lwt
44

55
let uri = Uri.of_string "http://mockbin.com/har" in
6-
let headers = Header.init ()
7-
|> fun h -> Header.add h "content-type" "application/x-www-form-urlencoded"
8-
in
6+
let headers = Header.add (Header.init ()) "content-type" "application/x-www-form-urlencoded" in
97
let body = Cohttp_lwt_body.of_string "foo=bar&hello=world" in
108

119
Client.call ~headers ~body `POST uri

test/fixtures/output/ocaml/cohttp/application-json.ml

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,7 @@ open Cohttp
33
open Lwt
44

55
let uri = Uri.of_string "http://mockbin.com/har" in
6-
let headers = Header.init ()
7-
|> fun h -> Header.add h "content-type" "application/json"
8-
in
6+
let headers = Header.add (Header.init ()) "content-type" "application/json" in
97
let body = Cohttp_lwt_body.of_string "{\"number\":1,\"string\":\"f\\\"oo\",\"arr\":[1,2,3],\"nested\":{\"a\":\"b\"},\"arr_mix\":[1,\"a\",{\"arr_mix_nested\":{}}]}" in
108

119
Client.call ~headers ~body `POST uri

test/fixtures/output/ocaml/cohttp/cookies.ml

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,7 @@ open Cohttp
33
open Lwt
44

55
let uri = Uri.of_string "http://mockbin.com/har" in
6-
let headers = Header.init ()
7-
|> fun h -> Header.add h "cookie" "foo=bar; bar=baz"
8-
in
6+
let headers = Header.add (Header.init ()) "cookie" "foo=bar; bar=baz" in
97

108
Client.call ~headers `POST uri
119
>>= fun (res, body_stream) ->

test/fixtures/output/ocaml/cohttp/full.ml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@ open Cohttp
33
open Lwt
44

55
let uri = Uri.of_string "http://mockbin.com/har?foo=bar&foo=baz&baz=abc&key=value" in
6-
let headers = Header.init ()
7-
|> fun h -> Header.add h "cookie" "foo=bar; bar=baz"
8-
|> fun h -> Header.add h "accept" "application/json"
9-
|> fun h -> Header.add h "content-type" "application/x-www-form-urlencoded"
10-
in
6+
let headers = Header.add_list (Header.init ()) [
7+
("cookie", "foo=bar; bar=baz");
8+
("accept", "application/json");
9+
("content-type", "application/x-www-form-urlencoded");
10+
] in
1111
let body = Cohttp_lwt_body.of_string "foo=bar" in
1212

1313
Client.call ~headers ~body `POST uri

test/fixtures/output/ocaml/cohttp/headers.ml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@ open Cohttp
33
open Lwt
44

55
let uri = Uri.of_string "http://mockbin.com/har" in
6-
let headers = Header.init ()
7-
|> fun h -> Header.add h "accept" "application/json"
8-
|> fun h -> Header.add h "x-foo" "Bar"
9-
in
6+
let headers = Header.add_list (Header.init ()) [
7+
("accept", "application/json");
8+
("x-foo", "Bar");
9+
] in
1010

1111
Client.call ~headers `GET uri
1212
>>= fun (res, body_stream) ->

test/fixtures/output/ocaml/cohttp/multipart-data.ml

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,7 @@ open Cohttp
33
open Lwt
44

55
let uri = Uri.of_string "http://mockbin.com/har" in
6-
let headers = Header.init ()
7-
|> fun h -> Header.add h "content-type" "multipart/form-data; boundary=---011000010111000001101001"
8-
in
6+
let headers = Header.add (Header.init ()) "content-type" "multipart/form-data; boundary=---011000010111000001101001" in
97
let body = Cohttp_lwt_body.of_string "-----011000010111000001101001\r\nContent-Disposition: form-data; name=\"foo\"; filename=\"hello.txt\"\r\nContent-Type: text/plain\r\n\r\nHello World\r\n-----011000010111000001101001--" in
108

119
Client.call ~headers ~body `POST uri

test/fixtures/output/ocaml/cohttp/multipart-file.ml

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,7 @@ open Cohttp
33
open Lwt
44

55
let uri = Uri.of_string "http://mockbin.com/har" in
6-
let headers = Header.init ()
7-
|> fun h -> Header.add h "content-type" "multipart/form-data; boundary=---011000010111000001101001"
8-
in
6+
let headers = Header.add (Header.init ()) "content-type" "multipart/form-data; boundary=---011000010111000001101001" in
97
let body = Cohttp_lwt_body.of_string "-----011000010111000001101001\r\nContent-Disposition: form-data; name=\"foo\"; filename=\"hello.txt\"\r\nContent-Type: text/plain\r\n\r\n\r\n-----011000010111000001101001--" in
108

119
Client.call ~headers ~body `POST uri

test/fixtures/output/ocaml/cohttp/multipart-form-data.ml

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,7 @@ open Cohttp
33
open Lwt
44

55
let uri = Uri.of_string "http://mockbin.com/har" in
6-
let headers = Header.init ()
7-
|> fun h -> Header.add h "content-type" "multipart/form-data; boundary=---011000010111000001101001"
8-
in
6+
let headers = Header.add (Header.init ()) "content-type" "multipart/form-data; boundary=---011000010111000001101001" in
97
let body = Cohttp_lwt_body.of_string "-----011000010111000001101001\r\nContent-Disposition: form-data; name=\"foo\"\r\n\r\nbar\r\n-----011000010111000001101001--" in
108

119
Client.call ~headers ~body `POST uri

test/fixtures/output/ocaml/cohttp/text-plain.ml

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,7 @@ open Cohttp
33
open Lwt
44

55
let uri = Uri.of_string "http://mockbin.com/har" in
6-
let headers = Header.init ()
7-
|> fun h -> Header.add h "content-type" "text/plain"
8-
in
6+
let headers = Header.add (Header.init ()) "content-type" "text/plain" in
97
let body = Cohttp_lwt_body.of_string "Hello World" in
108

119
Client.call ~headers ~body `POST uri

0 commit comments

Comments
 (0)