Skip to content

Commit 9c294c9

Browse files
committed
Merge pull request #1 from SGrondin/master
Added OCaml
2 parents f7e849b + eee6751 commit 9c294c9

4 files changed

Lines changed: 145 additions & 1 deletion

File tree

‎src/targets/ocaml/cohttp.js‎

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
'use strict';
2+
3+
var util = require('util');
4+
5+
module.exports = function (options) {
6+
var opts = util._extend({
7+
indent: ' '
8+
}, options);
9+
10+
var code = [];
11+
12+
code.push('open Cohttp_lwt_unix');
13+
code.push('open Lwt');
14+
code.push('');
15+
16+
// Create URI
17+
code.push(util.format('let uri = Uri.of_string "%s" in', this.source.fullUrl));
18+
19+
// Add headers, including the cookies
20+
var headersPresent = this.source.headers && this.source.headers.length;
21+
var cookiesPresent = this.source.cookies && this.source.cookies.length;
22+
23+
if (headersPresent || cookiesPresent) {
24+
code.push('let headers = Header.init ()');
25+
26+
if (headersPresent) {
27+
for (var header in this.source.headers) {
28+
code.push(util.format(opts.indent + '|> fun h -> Header.add h "%s" "%s"', this.source.headers[header].name, this.source.headers[header].value));
29+
}
30+
}
31+
32+
if (cookiesPresent) {
33+
var cookie = this.source.cookies.map(function(c){return c.name + '=' + c.value;}).join('; ');
34+
code.push(util.format(opts.indent + '|> fun h -> Header.add h "Cookie" "%s"', cookie));
35+
}
36+
code.push('in');
37+
}
38+
39+
// Add body
40+
var bodyPresent = this.source.postData && this.source.postData.text;
41+
if (bodyPresent) {
42+
//Just text
43+
code.push(util.format('let body = %s in', JSON.stringify(this.source.postData.text)));
44+
} else if (this.source.postData && !this.source.postData.text && this.source.postData.params && this.source.postData.params.length) {
45+
// Post params
46+
bodyPresent = true;
47+
var body = this.source.postData.params.map(function(p){return p.name + '=' + p.value;}).join('&');
48+
code.push(util.format('let body = "%s" in', body));
49+
}
50+
51+
// Do the request
52+
code.push('');
53+
code.push(util.format('Client.call %s%s(Code.method_of_string "%s") uri',
54+
(headersPresent || cookiesPresent) ? "~headers " : "",
55+
bodyPresent ? "~body " : "",
56+
this.source.method
57+
));
58+
59+
// Catch result
60+
code.push('>>= fun (res, body_stream) ->');
61+
code.push(opts.indent + '(* Do stuff with the result *)');
62+
63+
return code.join('\n');
64+
};
65+
66+
module.exports.info = function () {
67+
return {
68+
family: 'ocaml',
69+
key: 'cohttp',
70+
title: 'OCaml',
71+
link: 'https://github.com/mirage/ocaml-cohttp',
72+
description: 'Cohttp is a very lightweight HTTP server using Lwt or Async for OCaml',
73+
extname: '.ml'
74+
};
75+
};

‎src/targets/ocaml/index.js‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
'use strict';
2+
3+
module.exports = require('requireindex')(__dirname);
4+
5+
module.exports._familyInfo = function () {
6+
return {
7+
key: 'ocaml',
8+
title: 'OCaml',
9+
extname: '.ml',
10+
default: 'cohttp'
11+
};
12+
};

‎test/index.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ describe('HTTPSnippet', function () {
99
var targets = HTTPSnippet._targets().sort();
1010

1111
targets.should.be.an.Array;
12-
targets.should.eql(['curl', 'httpie', 'node', 'php', 'wget']);
12+
targets.should.eql(['curl', 'httpie', 'node', 'ocaml', 'php', 'wget']);
1313

1414
done();
1515
});

‎test/targets/ocaml/cohttp.js‎

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
'use strict';
2+
3+
var fixtures = require('../../fixtures');
4+
var HTTPSnippet = require('../../../src');
5+
var should = require('should');
6+
7+
describe('OCaml', function () {
8+
it('should convert full request to an OCaml snippet', function (done) {
9+
var result = new HTTPSnippet(fixtures.full).convert('ocaml', 'cohttp', {
10+
indent: '\t'
11+
});
12+
13+
result.replace(/\n/g, '').should.eql('open Cohttp_lwt_unixopen Lwtlet uri = Uri.of_string "http://httpconsole.com/debug?baz=abc&foo=bar&foo=baz" inlet headers = Header.init ()\t|> fun h -> Header.add h "Accept" "text/plain"\t|> fun h -> Header.add h "Content-Type" "application/json"\t|> fun h -> Header.add h "X-Pretty-Print" "2"\t|> fun h -> Header.add h "Cookie" "foo=bar; bar=baz"inlet body = "{\\"foo\\": \\"bar\\"}" inClient.call ~headers ~body (Code.method_of_string "POST") uri>>= fun (res, body_stream) ->\t(* Do stuff with the result *)');
14+
15+
done();
16+
});
17+
18+
it('should convert query request to an OCaml snippet', function (done) {
19+
var result = new HTTPSnippet(fixtures.query).convert('ocaml', 'cohttp', {
20+
indent: '\t'
21+
});
22+
23+
result.replace(/\n/g, '').should.eql('open Cohttp_lwt_unixopen Lwtlet uri = Uri.of_string "http://httpconsole.com/debug?key=value&baz=abc&foo=bar&foo=baz" inClient.call (Code.method_of_string "POST") uri>>= fun (res, body_stream) ->\t(* Do stuff with the result *)');
24+
25+
done();
26+
});
27+
28+
it('should convert simple request to an OCaml snippet', function (done) {
29+
var result = new HTTPSnippet(fixtures.simple).convert('ocaml', 'cohttp', {
30+
indent: '\t'
31+
});
32+
33+
result.replace(/\n/g, '').should.eql('open Cohttp_lwt_unixopen Lwtlet uri = Uri.of_string "http://httpconsole.com/debug?foo=bar" inlet headers = Header.init ()\t|> fun h -> Header.add h "Content-Type" "application/json"\t|> fun h -> Header.add h "Cookie" "bar=baz"inlet body = "{\\"foo\\": \\"bar\\"}" inClient.call ~headers ~body (Code.method_of_string "POST") uri>>= fun (res, body_stream) ->\t(* Do stuff with the result *)');
34+
35+
done();
36+
});
37+
38+
it('should convert short request to an OCaml snippet', function (done) {
39+
var result = new HTTPSnippet(fixtures.short).convert('ocaml', 'cohttp', {
40+
indent: '\t'
41+
});
42+
43+
result.replace(/\n/g, '').should.eql('open Cohttp_lwt_unixopen Lwtlet uri = Uri.of_string "http://httpconsole.com/echo" inClient.call (Code.method_of_string "GET") uri>>= fun (res, body_stream) ->\t(* Do stuff with the result *)');
44+
45+
done();
46+
});
47+
48+
it('should convert http1 request to an OCaml snippet', function (done) {
49+
var result = new HTTPSnippet(fixtures.http1).convert('ocaml', 'cohttp', {
50+
indent: '\t'
51+
});
52+
53+
result.replace(/\n/g, '').should.eql('open Cohttp_lwt_unixopen Lwtlet uri = Uri.of_string "http://httpconsole.com/debug" inClient.call (Code.method_of_string "GET") uri>>= fun (res, body_stream) ->\t(* Do stuff with the result *)');
54+
55+
done();
56+
});
57+
});

0 commit comments

Comments
 (0)