Skip to content
This repository was archived by the owner on Nov 8, 2024. It is now read-only.

Commit d9feea7

Browse files
author
Adam Kliment
committed
Merge pull request #7 from apiaryio/blueprint
API blueprint output format
2 parents d27c1dc + adadd66 commit d9feea7

File tree

6 files changed

+184
-33
lines changed

6 files changed

+184
-33
lines changed

README.md

Lines changed: 62 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ The only glitch is that cURL `--trace` saves data in [its custom format][gist],
2020
$ curl --trace - http://httpbin.org/ip | curl-trace-parser
2121
```
2222

23-
## Example
23+
## Sample API
2424

2525
We will be using this [sample API][apiarydoc] created with the [Apiary.io mock server](http://apiary.io) to demonstrate tracing an HTTP communication and the use of the cURL trace parser.
2626

@@ -45,51 +45,89 @@ Note this cURL example is copied and pasted from [Apiary interactive API documen
4545

4646
[example]: http://docs.curltraceparser.apiary.io/#get-%2Fshopping-cart
4747

48-
## Parse the trace file from command line
48+
## Examples
49+
50+
### `--raw` format
51+
52+
The output is ASCII representation of a raw [HTTP message][message] with few modifications:
53+
54+
- Request line begins with `> `
55+
- Response line begins with `< `
56+
- Request and Response is delimited by CR+LF
57+
- Both Request and Response are terminated by an extra trailing LF
58+
59+
Note: This is little bit tricky because HTTP RFC does not have declared delimiter for Request and Response, for obvious reasons.
4960

5061
```bash
51-
$ cat tracefile | curl-trace-parser
52-
> GET /shopping-cart HTTP/1.1
62+
$ cat tracefile | curl-trace-parser --raw
63+
> POST /shopping-cart HTTP/1.1
5364
> User-Agent: curl/7.24.0 (x86_64-apple-darwin12.0) libcurl/7.24.0 OpenSSL/0.9.8x zlib/1.2.5
5465
> Host: curltraceparser.apiary.io
5566
> Accept: */*
56-
>
67+
> Content-Type: application/json
68+
> Content-Length: 39
5769
>
70+
> { "product":"1AB23ORM", "quantity": 2 }
5871

59-
< HTTP/1.1 200 OK
72+
< HTTP/1.1 201 Created
6073
< Content-Type: application/json
61-
< Date: Sun, 21 Jul 2013 13:23:55 GMT
74+
< Date: Tue, 30 Jul 2013 11:32:30 GMT
6275
< X-Apiary-Ratelimit-Limit: 120
6376
< X-Apiary-Ratelimit-Remaining: 119
64-
< Content-Length: 119
77+
< Content-Length: 50
6578
< Connection: keep-alive
6679
<
67-
< { "items": [
68-
< { "url": "/shopping-cart/1", "product":"2ZY48XPZ", "quantity": 1, "name": "New socks", "price": 1.25 }
69-
< ] }
80+
< { "status": "created", "url": "/shopping-cart/2" }
81+
```
82+
83+
### `--blueprint` format
84+
85+
The output is HTTP Request and Response in the [API blueprint format](http://apiblueprint.org) which is the superset of markdown.
86+
87+
```
88+
$ cat tracefile | ./bin/curl-trace-parser --blueprint
89+
# POST /shopping-cart
90+
+ Request
91+
+ Headers
92+
93+
User-Agent:curl/7.24.0 (x86_64-apple-darwin12.0) libcurl/7.24.0 OpenSSL/0.9.8x zlib/1.2.5
94+
Host:curltraceparser.apiary.io
95+
Accept:*/*
96+
Content-Type:application/json
97+
Content-Length:39
98+
99+
+ Body
100+
101+
{ "product":"1AB23ORM", "quantity": 2 }
102+
103+
+ Response 201
104+
+ Headers
105+
106+
Content-Type:application/json
107+
Date:Tue, 30 Jul 2013 11:32:30 GMT
108+
X-Apiary-Ratelimit-Limit:120
109+
X-Apiary-Ratelimit-Remaining:119
110+
Content-Length:50
111+
Connection:keep-alive
112+
113+
+ Body
114+
115+
{ "status": "created", "url": "/shopping-cart/2" }
116+
70117
```
71118
72-
## Parse the trace file using Node.JS
119+
Note: This format does not expect any CR+LF in body
120+
121+
### Parse the trace to raw HTTP file using Node.JS
73122
74123
```javascript
75124
var fs = require('fs');
76125
var parser = require('curl-trace-parser');
77126
fs.readFile('./tracefile', 'utf8', function (err,trace) {
78-
console.log(parser.parseToString(trace));
127+
console.log(parser.parse(trace));
79128
})
80129
```
81130
82-
## Output format
83-
84-
The output is ASCII representation of a raw [HTTP message][message] with few modifications:
85-
86-
- Request line begins with `> `
87-
- Response line begins with `< `
88-
- Request and response is delimited by CR+LF
89-
- Both Request and Response are terminated by an extra trailing LF
90-
91-
Note: This is little bit tricky because HTTP RFC does not have declared delimiter for Request and Response, for obvious reasons.
92-
93131
## Output format reverse parser
94132
95133
```javascript

bin/curl-trace-parser

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,43 @@
11
#!/usr/bin/env node
22

33
var cmd = require('commander');
4-
var parser = require('../lib/parser');
5-
cmd.version('0.0.1');
4+
var curl = require('../lib/parser');
5+
var http = require('http-string-parser');
6+
var bf = require('api-blueprint-http-formatter')
67

78
var stdin = "";
89

910
process.stdin.resume();
1011
process.stdin.setEncoding('utf8');
1112

13+
cmd.command('curl-trace-parser')
14+
.description('Parse output form curl --trace option')
15+
.version('0.0.8')
16+
.option('-r, --raw', 'Output raw data (Default)')
17+
.option('-b, --blueprint', 'Output in API blueprint format')
18+
.parse(process.argv);
19+
1220
process.stdin.on('data', function(data) {
1321
stdin += data;
1422
});
1523

1624
process.stdin.on('end', function() {
17-
parserOutput = parser.parseToString(stdin)
18-
process.stdout.write(parserOutput);
25+
//TODO commander options does not work here :(
26+
var option = process.argv[2];
27+
28+
if(option == '-r' || option == '--raw' || option == undefined){
29+
var parserOutput = curl.parseToString(stdin);
30+
process.stdout.write(parserOutput);
31+
}
32+
if(option == 'b' || option == '--blueprint'){
33+
var rawPair = curl.parse(stdin);
34+
parsedPair = {
35+
'request': http.parseRequest(rawPair['request']),
36+
'response': http.parseResponse(rawPair['response'])
37+
}
38+
var blueprint = bf.format(parsedPair);
39+
process.stdout.write(blueprint);
40+
}
1941
process.exit(0)
2042
});
2143

package.json

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "curl-trace-parser",
3-
"version": "0.0.7",
3+
"version": "0.0.8",
44
"description": "Parse curl --trace option output to raw HTTP message",
55
"main": "lib/parser.js",
66
"scripts": {
@@ -11,7 +11,9 @@
1111
"curl-trace-parser": "bin/curl-trace-parser"
1212
},
1313
"dependencies": {
14-
"commander": "1.2.0"
14+
"commander": "1.2.0",
15+
"api-blueprint-http-formatter": "0.0.1",
16+
"http-string-parser": "0.0.4"
1517
},
1618
"devDependencies": {
1719
"coffee-script": "1.6.3",
@@ -26,7 +28,10 @@
2628
"http",
2729
"message",
2830
"request",
29-
"repsponse"
31+
"repsponse",
32+
"api",
33+
"blueprint",
34+
"apiary"
3035
],
3136
"author": "Adam Kliment <adam@apiary.io>",
3237
"license": "MIT",
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# GET /shopping-cart
2+
+ Request
3+
+ Headers
4+
5+
User-Agent:curl/7.24.0 (x86_64-apple-darwin12.0) libcurl/7.24.0 OpenSSL/0.9.8x zlib/1.2.5
6+
Host:curltraceparser.apiary.io
7+
Accept:*/*
8+
9+
+ Body
10+
11+
12+
13+
+ Response 200
14+
+ Headers
15+
16+
Content-Type:application/json
17+
Date:Sun, 21 Jul 2013 13:23:55 GMT
18+
X-Apiary-Ratelimit-Limit:120
19+
X-Apiary-Ratelimit-Remaining:119
20+
Content-Length:119
21+
Connection:keep-alive
22+
23+
+ Body
24+
25+
{ "items": [
26+
{ "url": "/shopping-cart/1", "product":"2ZY48XPZ", "quantity": 1, "name": "New socks", "price": 1.25 }
27+
] }
28+
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# POST /shopping-cart
2+
+ Request
3+
+ Headers
4+
5+
User-Agent:curl/7.24.0 (x86_64-apple-darwin12.0) libcurl/7.24.0 OpenSSL/0.9.8x zlib/1.2.5
6+
Host:curltraceparser.apiary.io
7+
Accept:*/*
8+
Content-Type:application/json
9+
Content-Length:39
10+
11+
+ Body
12+
13+
{ "product":"1AB23ORM", "quantity": 2 }
14+
15+
+ Response 201
16+
+ Headers
17+
18+
Content-Type:application/json
19+
Date:Sun, 21 Jul 2013 14:51:09 GMT
20+
X-Apiary-Ratelimit-Limit:120
21+
X-Apiary-Ratelimit-Remaining:119
22+
Content-Length:50
23+
Connection:keep-alive
24+
25+
+ Body
26+
27+
{ "status": "created", "url": "/shopping-cart/2" }
28+

test/integration/cli-test.coffee

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,14 @@ describe "Command line", () ->
1313
done error
1414
done()
1515

16-
describe "parsing from standard input", () ->
16+
describe "parsing from standard input with --raw", () ->
1717
stdout = ""
1818
stderr = ""
1919
exitStatus = ""
2020

2121
before (done) ->
2222
cmd = 'cat ./test/fixtures/get/tracefile | ' +
23-
'./bin/curl-trace-parser'
23+
'./bin/curl-trace-parser --raw'
2424

2525
cli = exec cmdPrefix + cmd, (error, out, err) ->
2626
stdout = out
@@ -43,6 +43,36 @@ describe "Command line", () ->
4343
assert.equal stdout, expected
4444
done()
4545

46+
describe "parsing from standard input with --blueprint option", () ->
47+
stdout = ""
48+
stderr = ""
49+
exitStatus = ""
50+
51+
before (done) ->
52+
cmd = 'cat ./test/fixtures/post/tracefile | ' +
53+
'./bin/curl-trace-parser --blueprint'
54+
55+
cli = exec cmdPrefix + cmd, (error, out, err) ->
56+
stdout = out
57+
stderr = err
58+
if error
59+
exitStatus = error.status
60+
done()
61+
62+
cli.on 'exit', (code) ->
63+
exitStatus = code
64+
65+
it "should not return nothing to stderr", () ->
66+
assert.equal stderr, ""
67+
68+
it "should return with exit code 0", () ->
69+
70+
it "should return parsed body in API Blueprint format to standard output", (done) ->
71+
expectedOutputPath = "./test/fixtures/post/expected-output.md"
72+
fs.readFile expectedOutputPath, 'utf8', (err, expected) ->
73+
assert.equal stdout, expected
74+
done()
75+
4676
describe "no input on stdin and no options", ()->
4777

4878
stdout = ""

0 commit comments

Comments
 (0)