Skip to content

Commit 06d01c3

Browse files
committed
adding har validation
1 parent 2b6f221 commit 06d01c3

5 files changed

Lines changed: 83 additions & 42 deletions

File tree

bin/httpsnippet

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@
22

33
'use strict';
44

5-
var cmd = require('commander');
65
var async = require('async');
6+
var chalk = require('chalk');
7+
var cmd = require('commander');
78
var debug = require('debug')('httpsnippet');
89
var fs = require('fs');
910
var HTTPSnippet = require('../src');
1011
var path = require('path');
1112
var pkg = require('../package.json');
12-
var chalk = require('chalk');
1313

1414
cmd
1515
.version(pkg.version)
@@ -65,7 +65,7 @@ async.waterfall([
6565
cb(null, JSON.parse(buffer));
6666
} catch (e) {
6767
debug('failed to parse source json');
68-
cb('fail', null);
68+
cb('failed to parse source json', null);
6969
}
7070
};
7171

@@ -76,7 +76,16 @@ async.waterfall([
7676

7777
function snippet (files, sources, next) {
7878
var iterator = function (source, cb) {
79-
var snippet = new HTTPSnippet(source);
79+
var snippet;
80+
81+
try {
82+
snippet = new HTTPSnippet(source);
83+
} catch (e) {
84+
debug(e);
85+
86+
return cb(!e[0] ? 'invalid input' : (e[0].field + ' ' + e[0].message), null);
87+
}
88+
8089
cb(null, snippet.convert(cmd.family, cmd.target));
8190
};
8291

@@ -109,10 +118,14 @@ async.waterfall([
109118
if (!cmd.output) {
110119
var iterator = function (file) {
111120
var index = files.indexOf(file);
112-
console.log(chalk.cyan.bold.underline(file), chalk.cyan.bold(':'), '\n', snippets[index], '\n');
121+
console.log('%s:\n%s\n', chalk.cyan.bold.underline(file), snippets[index]);
113122
};
114123

115124
async.each(files, iterator);
116125
}
117126
}
118-
]);
127+
], function (err, result) {
128+
if (err) {
129+
console.log('%s: %s', chalk.red.bold('ERROR'), err);
130+
}
131+
});

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
"html-cov": "mocha -r blanket -R html-cov > test/coverage.html",
1111
"codeclimate": "codeclimate < test/lcov.info"
1212
},
13-
"config":{
13+
"config": {
1414
"blanket": {
1515
"pattern": "src"
1616
}
@@ -36,6 +36,7 @@
3636
"chalk": "^1.0.0",
3737
"commander": "^2.6.0",
3838
"debug": "^2.1.1",
39+
"har-validator": "^1.1.2",
3940
"requireindex": "^1.1.0"
4041
}
4142
}

src/index.js

Lines changed: 47 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -5,55 +5,70 @@ var mapper = require('./mapper');
55
var qs = require('querystring');
66
var targets = require('./targets');
77
var url = require('url');
8+
var validate = require('har-validator');
89
var util = require('util');
910

1011
// constructor
1112
var HTTPSnippet = function (req, lang) {
1213
this.source = util._extend({}, req);
1314

14-
// construct query string object
15-
this.source.queryObj = {};
16-
this.source.headersObj = {};
15+
// add optional properties to make validation successful
16+
this.source.httpVersion = this.source.httpVersion || 'HTTP/1.1';
17+
this.source.queryString = this.source.queryString || [];
18+
this.source.headers = this.source.headers || [];
19+
this.source.cookies = this.source.cookies || [];
20+
this.source.postData = this.source.postData || {};
21+
this.source.postData.mimeType = this.source.postData.mimeType || 'application/x-www-form-urlencoded';
22+
23+
this.source.bodySize = 0;
24+
this.source.headersSize = 0;
25+
this.source.postData.size = 0;
26+
27+
validate.request(this.source, function (err, valid) {
28+
if (!valid) {
29+
throw err;
30+
}
1731

18-
if (this.source.url === undefined) {
19-
throw new Error('a request url is required');
20-
}
32+
// construct query string object
33+
this.source.queryObj = {};
34+
this.source.headersObj = {};
2135

22-
// construct query objects
23-
if (this.source.queryString && this.source.queryString.length) {
24-
debug('queryString found, constructing queryString pair map');
36+
// construct query objects
37+
if (this.source.queryString && this.source.queryString.length) {
38+
debug('queryString found, constructing queryString pair map');
2539

26-
this.source.queryString.map(mapper(this.source.queryObj));
27-
}
40+
this.source.queryString.map(mapper(this.source.queryObj));
41+
}
2842

29-
// construct headers objects
30-
if (this.source.headers && this.source.headers.length) {
31-
debug('headers found, constructing header pair map');
43+
// construct headers objects
44+
if (this.source.headers && this.source.headers.length) {
45+
debug('headers found, constructing header pair map');
3246

33-
this.source.headers.map(mapper(this.source.headersObj));
34-
}
47+
this.source.headers.map(mapper(this.source.headersObj));
48+
}
3549

36-
// deconstruct the uri
37-
this.source.uriObj = url.parse(this.source.url, true, true);
50+
// deconstruct the uri
51+
this.source.uriObj = url.parse(this.source.url, true, true);
3852

39-
// merge all possible queryString values
40-
this.source.queryString = util._extend(this.source.uriObj.query, this.source.queryObj);
53+
// merge all possible queryString values
54+
this.source.queryString = util._extend(this.source.uriObj.query, this.source.queryObj);
4155

42-
// reset uriObj values for a clean url
43-
this.source.uriObj.query = null;
44-
this.source.uriObj.search = null;
45-
this.source.uriObj.path = this.source.uriObj.pathname;
56+
// reset uriObj values for a clean url
57+
this.source.uriObj.query = null;
58+
this.source.uriObj.search = null;
59+
this.source.uriObj.path = this.source.uriObj.pathname;
4660

47-
// keep the base url clean of queryString
48-
this.source.url = url.format(this.source.uriObj);
61+
// keep the base url clean of queryString
62+
this.source.url = url.format(this.source.uriObj);
4963

50-
// update the uri object
51-
this.source.uriObj.query = this.source.queryString;
52-
this.source.uriObj.search = qs.stringify(this.source.queryString);
53-
this.source.uriObj.path = this.source.uriObj.pathname + '?' + this.source.uriObj.search;
64+
// update the uri object
65+
this.source.uriObj.query = this.source.queryString;
66+
this.source.uriObj.search = qs.stringify(this.source.queryString);
67+
this.source.uriObj.path = this.source.uriObj.pathname + '?' + this.source.uriObj.search;
5468

55-
// construct a full url
56-
this.source.fullUrl = url.format(this.source.uriObj);
69+
// construct a full url
70+
this.source.fullUrl = url.format(this.source.uriObj);
71+
}.bind(this));
5772
};
5873

5974
HTTPSnippet.prototype.getSource = function () {

test/fixtures/invalid.json

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"method": "POST",
3+
"url": "http://httpconsole.com/debug",
4+
"httpVersion": "HTTP/1.1",
5+
"queryString": [],
6+
"cookies": [],
7+
"postData": {
8+
"size": 14,
9+
"mimeType": "application/json",
10+
"text": "{\"foo\": \"bar\"}"
11+
}
12+
}

test/targets/curl.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ describe('cURL', function () {
1111
});
1212

1313
result.should.be.a.String;
14-
result.should.eql('curl --request POST --url "http://httpconsole.com/debug?foo=bar" --cookie "bar=baz" --header "Content-Type: application/json" --data "{\\"foo\\": \\"bar\\"}"');
14+
result.should.eql('curl --request POST --url "http://httpconsole.com/debug?foo=bar" --cookie "bar=baz" --header "Content-Type: application/json" --form "{\\"foo\\": \\"bar\\"}"');
1515

1616
done();
1717
});
@@ -23,7 +23,7 @@ describe('cURL', function () {
2323
});
2424

2525
result.should.be.a.String;
26-
result.should.eql('curl -X POST "http://httpconsole.com/debug?foo=bar" -b "bar=baz" -H "Content-Type: application/json" -d "{\\"foo\\": \\"bar\\"}"');
26+
result.should.eql('curl -X POST "http://httpconsole.com/debug?foo=bar" -b "bar=baz" -H "Content-Type: application/json" -F "{\\"foo\\": \\"bar\\"}"');
2727

2828
done();
2929
});
@@ -45,7 +45,7 @@ describe('cURL', function () {
4545
});
4646

4747
result.should.be.a.String;
48-
result.replace(/\\\n/g, '').should.eql('curl --request POST @--url "http://httpconsole.com/debug?foo=bar" @--cookie "bar=baz" @--header "Content-Type: application/json" @--data "{\\"foo\\": \\"bar\\"}"');
48+
result.replace(/\\\n/g, '').should.eql('curl --request POST @--url "http://httpconsole.com/debug?foo=bar" @--cookie "bar=baz" @--header "Content-Type: application/json" @--form "{\\"foo\\": \\"bar\\"}"');
4949

5050
done();
5151
});

0 commit comments

Comments
 (0)