test-progress.coffee
1.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
Promise = require "bluebird"
bhttp = require "./"
fs = require "fs"
devNull = require "dev-null"
formatLine = (line) -> line.toString().replace(/\n/g, "\\n").replace(/\r/g, "\\r")
Promise.try ->
# multipart POST upload
console.log "Starting 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"}
onUploadProgress: (completedBytes, totalBytes, request) ->
console.log "#{completedBytes / totalBytes * 100}%", completedBytes, totalBytes
.then (response) ->
console.log "POST multipart", formatLine(response.body)
.then ->
# GET large file
console.log "Starting download..."
bhttp.get "http://posttestserver.com/files/2015/04/06/f_00.16.102133822615",
headers: {"user-agent": "bhttp/test GET large file"}
stream: true
.then (response) ->
#setTimeout (->), 10000
console.log "Got response"
response.on "progress", (completedBytes, totalBytes, request) ->
console.log "#{completedBytes / totalBytes * 100}%", completedBytes, totalBytes
response.pipe(devNull())
response.on "end", ->
console.log "Completed response download"