remove hard-coded json content-type for streaming http output
There was a stale assumption that streaming output from a channel would always be json. This commit removes that code, allowing Content-Type to appropriately be set like other, non-channel-streaming commands. License: MIT Signed-off-by: Cayman Nava <caymannava@gmail.com>
Cayman Nava committed
Sep 19, 2015 at 12:11 UTC
9634b25b632692f603fb82ebe60fec13feb1e61c
2 files changed
+61
-5
commands/http/handler.go
-5
@@ -224,13 +224,8 @@ func sendResponse(w http.ResponseWriter, r *http.Request, res cmds.Response, req
224
_, isChan = res.Output().(<-chan interface{})
225
}
226
227
- streamChans, _, _ := req.Option("stream-channels").Bool()
227
if isChan {
228
h.Set(channelHeader, "1")
230
- if streamChans {
231
- // streaming output from a channel will always be json objects
232
- mime = applicationJson
233
- }
229
}
230
231
if mime != "" {
test/sharness/t0230-channel-streaming-http-content-type.sh
new
+61
@@ -0,0 +1,61 @@
1
+#!/bin/sh
2
+#
3
+# Copyright (c) 2015 Cayman Nava
4
+# MIT Licensed; see the LICENSE file in this repository.
5
+#
6
+
7
+test_description="Test Content-Type for channel-streaming commands"
8
+
9
+. lib/test-lib.sh
10
+
11
+test_init_ipfs
12
+
13
+test_ls_cmd() {
14
+
15
+ test_expect_success "Text encoded channel-streaming command succeeds" '
16
+ mkdir -p testdir &&
17
+ echo "hello test" >testdir/test.txt &&
18
+ ipfs add -r testdir &&
19
+ curl -i "http://localhost:$PORT_API/api/v0/refs?arg=QmTcJAn3JP8ZMAKS6WS75q8sbTyojWKbxcUHgLYGWur4Ym&stream-channels=true&encoding=text" >actual_output
20
+ '
21
+
22
+ test_expect_success "Text encoded channel-streaming command output looks good" '
23
+ printf "HTTP/1.1 200 OK\r\n" >expected_output &&
24
+ printf "Content-Type: text/plain\r\n" >>expected_output &&
25
+ printf "Transfer-Encoding: chunked\r\n" >>expected_output &&
26
+ printf "X-Chunked-Output: 1\r\n" >>expected_output &&
27
+ printf "\r\n" >>expected_output &&
28
+ echo QmRmPLc1FsPAn8F8F9DQDEYADNX5ER2sgqiokEvqnYknVW >>expected_output &&
29
+ test_cmp expected_output actual_output
30
+ '
31
+
32
+ test_expect_success "JSON encoded channel-streaming command succeeds" '
33
+ mkdir -p testdir &&
34
+ echo "hello test" >testdir/test.txt &&
35
+ ipfs add -r testdir &&
36
+ curl -i "http://localhost:$PORT_API/api/v0/refs?arg=QmTcJAn3JP8ZMAKS6WS75q8sbTyojWKbxcUHgLYGWur4Ym&stream-channels=true&encoding=json" >actual_output
37
+ '
38
+
39
+ test_expect_success "JSON encoded channel-streaming command output looks good" '
40
+ printf "HTTP/1.1 200 OK\r\n" >expected_output &&
41
+ printf "Content-Type: application/json\r\n" >>expected_output &&
42
+ printf "Transfer-Encoding: chunked\r\n" >>expected_output &&
43
+ printf "X-Chunked-Output: 1\r\n" >>expected_output &&
44
+ printf "\r\n" >>expected_output &&
45
+ cat <<-\EOF >>expected_output &&
46
+ {
47
+ "Ref": "QmRmPLc1FsPAn8F8F9DQDEYADNX5ER2sgqiokEvqnYknVW",
48
+ "Err": ""
49
+ }
50
+ EOF
51
+ perl -pi -e '"'"'chomp if eof'"'"' expected_output &&
52
+ test_cmp expected_output actual_output
53
+ '
54
+}
55
+
56
+# should work online (only)
57
+test_launch_ipfs_daemon
58
+test_ls_cmd
59
+test_kill_ipfs_daemon
60
+
61
+test_done