Skip to content
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
75 changes: 75 additions & 0 deletions src/targets/ocaml/cohttp.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
'use strict';

var util = require('util');

module.exports = function (options) {
var opts = util._extend({
indent: ' '
}, options);

var code = [];

code.push('open Cohttp_lwt_unix');
code.push('open Lwt');
code.push('');

// Create URI
code.push(util.format('let uri = Uri.of_string "%s" in', this.source.fullUrl));

// Add headers, including the cookies
var headersPresent = this.source.headers && this.source.headers.length;
var cookiesPresent = this.source.cookies && this.source.cookies.length;

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

you don't need this anymore, checkout the most recent update: v1.0.0-beta.6


if (headersPresent || cookiesPresent) {
code.push('let headers = Header.init ()');

if (headersPresent) {
for (var header in this.source.headers) {
code.push(util.format(opts.indent + '|> fun h -> Header.add h "%s" "%s"', this.source.headers[header].name, this.source.headers[header].value));
}
}

if (cookiesPresent) {
var cookie = this.source.cookies.map(function(c){return c.name + '=' + c.value;}).join('; ');
code.push(util.format(opts.indent + '|> fun h -> Header.add h "Cookie" "%s"', cookie));
}
code.push('in');
}

// Add body
var bodyPresent = this.source.postData && this.source.postData.text;
if (bodyPresent) {
//Just text
code.push(util.format('let body = %s in', JSON.stringify(this.source.postData.text)));
} else if (this.source.postData && !this.source.postData.text && this.source.postData.params && this.source.postData.params.length) {
// Post params
bodyPresent = true;
var body = this.source.postData.params.map(function(p){return p.name + '=' + p.value;}).join('&');
code.push(util.format('let body = "%s" in', body));
}

// Do the request
code.push('');
code.push(util.format('Client.call %s%s(Code.method_of_string "%s") uri',
(headersPresent || cookiesPresent) ? "~headers " : "",
bodyPresent ? "~body " : "",
this.source.method
));

// Catch result
code.push('>>= fun (res, body_stream) ->');
code.push(opts.indent + '(* Do stuff with the result *)');

return code.join('\n');
};

module.exports.info = function () {
return {
family: 'ocaml',
key: 'cohttp',
title: 'OCaml',
link: 'https://github.com/mirage/ocaml-cohttp',
description: 'Cohttp is a very lightweight HTTP server using Lwt or Async for OCaml',
extname: '.ml'
};
};
12 changes: 12 additions & 0 deletions src/targets/ocaml/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
'use strict';

module.exports = require('requireindex')(__dirname);

module.exports._familyInfo = function () {
return {
key: 'ocaml',
title: 'OCaml',
extname: '.ml',
default: 'cohttp'
};
};
2 changes: 1 addition & 1 deletion test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ describe('HTTPSnippet', function () {
var targets = HTTPSnippet._targets().sort();

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

done();
});
Expand Down
57 changes: 57 additions & 0 deletions test/targets/ocaml/cohttp.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
'use strict';

var fixtures = require('../../fixtures');
var HTTPSnippet = require('../../../src');
var should = require('should');

describe('OCaml', function () {
it('should convert full request to an OCaml snippet', function (done) {
var result = new HTTPSnippet(fixtures.full).convert('ocaml', 'cohttp', {
indent: '\t'
});

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 *)');

done();
});

it('should convert query request to an OCaml snippet', function (done) {
var result = new HTTPSnippet(fixtures.query).convert('ocaml', 'cohttp', {
indent: '\t'
});

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 *)');

done();
});

it('should convert simple request to an OCaml snippet', function (done) {
var result = new HTTPSnippet(fixtures.simple).convert('ocaml', 'cohttp', {
indent: '\t'
});

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 *)');

done();
});

it('should convert short request to an OCaml snippet', function (done) {
var result = new HTTPSnippet(fixtures.short).convert('ocaml', 'cohttp', {
indent: '\t'
});

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 *)');

done();
});

it('should convert http1 request to an OCaml snippet', function (done) {
var result = new HTTPSnippet(fixtures.http1).convert('ocaml', 'cohttp', {
indent: '\t'
});

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 *)');

done();
});
});