test-bhttp.coffee
4.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
bhttp = require "./"
Promise = require "bluebird"
fs = require "fs"
util = require "util"
devNull = require "dev-null"
requestbinTarget = process.argv[2]
if not requestbinTarget
throw new Error("No RequestBin target URL specified.")
formatLine = (line) -> line.toString().replace(/\n/g, "\\n").replace(/\r/g, "\\r")
testcases = []
testcases.push(Promise.try ->
# multipart POST upload
bhttp.post "http://posttestserver.com/post.php",
fieldOne: "value 1"
fieldTwo: "value 2"
fieldThree: ["value 3a", "value 3b"]
fieldFour: new Buffer "value 4"
testFile: fs.createReadStream("./lower.txt")
, {headers: {"user-agent": "bhttp/test POST multipart"}}
.then (response) ->
console.log "POST multipart", formatLine(response.body)
)
testcases.push(Promise.try ->
# url-encoded POST form
bhttp.post requestbinTarget,
fieldOne: "value 1"
fieldTwo: "value 2"
fieldThree: ["value 3a", "value 3b"]
, {headers: {"user-agent": "bhttp/test POST url-encoded"}}
.then (response) ->
console.log "POST url-encoded", formatLine(response.body)
)
testcases.push(Promise.try ->
# POST stream
bhttp.post requestbinTarget, null,
inputStream: fs.createReadStream("./lower.txt")
headers: {"user-agent": "bhttp/test POST stream"}
.then (response) ->
console.log "POST stream", formatLine(response.body)
)
testcases.push(Promise.try ->
# POST buffer
bhttp.post requestbinTarget, null,
inputBuffer: new Buffer("test buffer contents")
headers: {"user-agent": "bhttp/test POST buffer"}
.then (response) ->
console.log "POST buffer", formatLine(response.body)
)
testcases.push(Promise.try ->
# PUT buffer
bhttp.put requestbinTarget, null,
inputBuffer: new Buffer("test buffer contents")
headers: {"user-agent": "bhttp/test PUT buffer"}
.then (response) ->
console.log "PUT buffer", formatLine(response.body)
)
testcases.push(Promise.try ->
# PATCH buffer
bhttp.patch requestbinTarget, null,
inputBuffer: new Buffer("test buffer contents")
headers: {"user-agent": "bhttp/test PATCH buffer"}
.then (response) ->
console.log "PATCH buffer", formatLine(response.body)
)
testcases.push(Promise.try ->
# DELETE
bhttp.delete requestbinTarget, {headers: {"user-agent": "bhttp/test DELETE"}}
.then (response) ->
console.log "DELETE", formatLine(response.body)
)
testcases.push(Promise.try ->
# GET
bhttp.get requestbinTarget, {headers: {"user-agent": "bhttp/test GET"}}
.then (response) ->
console.log "GET", formatLine(response.body)
)
testcases.push(Promise.try ->
# Cookie test
session = bhttp.session()
session.post "http://www.html-kit.com/tools/cookietester/",
cn: "testkey1"
cv: "testvalue1"
.then (response) ->
console.log "COOKIE1", response.redirectHistory[0].headers["set-cookie"]
# Try to create a new session, make sure cookies don't carry over
session = bhttp.session(headers: {"x-test-header": "some value"})
session.post "http://www.html-kit.com/tools/cookietester/",
cn: "testkey2"
cv: "testvalue2"
.then (response) ->
console.log "COOKIE2c", response.redirectHistory[0].headers["set-cookie"]
console.log "COOKIE2h", response.request.options.headers
console.log "COOKIE2h", response.redirectHistory[0].request.options.headers
# Last test... without a session, this time
bhttp.post "http://www.html-kit.com/tools/cookietester/",
cn: "testkey3"
cv: "testvalue4"
.then (response) ->
console.log "NON-COOKIE SET", response.redirectHistory[0].headers["set-cookie"]
# Ensure nothing was retained cookie-wise
bhttp.get "http://www.html-kit.com/tools/cookietester/"
.then (response) ->
console.log "NON-COOKIE GET", response.request.options.headers["cookie"]
)
testcases.push(Promise.try ->
# No-redirect test
session = bhttp.session()
session.post "http://www.html-kit.com/tools/cookietester/",
cn: "testkey1"
cv: "testvalue1"
, followRedirects: false
.then (response) ->
console.log "NO-REDIR", response.headers["location"]
)
testcases.push(Promise.try ->
# Redirect test
bhttp.get "http://google.com/"
.then (response) ->
console.log "REDIR", response.headers["location"], response.redirectHistory.length
)
Promise.all testcases
.then ->
bhttp.get "http://posttestserver.com/files/2015/01/19/f_03.01.01627545156", stream: true
.then (response) ->
#response.pipe process.stdout
response
.pipe(devNull())
.on "finish", ->
console.log "GET STREAM", "ok"