| 1 | package cli |
| 2 | |
| 3 | import ( |
| 4 | "net/http" |
| 5 | "testing" |
| 6 | |
| 7 | "github.com/ipfs/kubo/test/cli/harness" |
| 8 | "github.com/stretchr/testify/assert" |
| 9 | "github.com/stretchr/testify/require" |
| 10 | ) |
| 11 | |
| 12 | // TestRPCGetContentType verifies that the RPC endpoint for `ipfs get` returns |
| 13 | // the correct Content-Type header based on output format options. |
| 14 | // |
| 15 | // Output formats and expected Content-Type: |
| 16 | // - default (no flags): tar (transport format) -> application/x-tar |
| 17 | // - --archive: tar archive -> application/x-tar |
| 18 | // - --compress: gzip -> application/gzip |
| 19 | // - --archive --compress: tar.gz -> application/gzip |
| 20 | // |
| 21 | // Fixes: https://github.com/ipfs/kubo/issues/2376 |
| 22 | func TestRPCGetContentType(t *testing.T) { |
| 23 | t.Parallel() |
| 24 | |
| 25 | node := harness.NewT(t).NewNode().Init() |
| 26 | node.StartDaemon("--offline") |
| 27 | |
| 28 | // add test content |
| 29 | cid := node.IPFSAddStr("test content for Content-Type header verification") |
| 30 | |
| 31 | tests := []struct { |
| 32 | name string |
| 33 | query string |
| 34 | expectedContentType string |
| 35 | }{ |
| 36 | { |
| 37 | name: "default returns application/x-tar", |
| 38 | query: "?arg=" + cid, |
| 39 | expectedContentType: "application/x-tar", |
| 40 | }, |
| 41 | { |
| 42 | name: "archive=true returns application/x-tar", |
| 43 | query: "?arg=" + cid + "&archive=true", |
| 44 | expectedContentType: "application/x-tar", |
| 45 | }, |
| 46 | { |
| 47 | name: "compress=true returns application/gzip", |
| 48 | query: "?arg=" + cid + "&compress=true", |
| 49 | expectedContentType: "application/gzip", |
| 50 | }, |
| 51 | { |
| 52 | name: "archive=true&compress=true returns application/gzip", |
| 53 | query: "?arg=" + cid + "&archive=true&compress=true", |
| 54 | expectedContentType: "application/gzip", |
| 55 | }, |
| 56 | } |
| 57 | |
| 58 | for _, tt := range tests { |
| 59 | t.Run(tt.name, func(t *testing.T) { |
| 60 | url := node.APIURL() + "/api/v0/get" + tt.query |
| 61 | |
| 62 | req, err := http.NewRequest(http.MethodPost, url, nil) |
| 63 | require.NoError(t, err) |
| 64 | |
| 65 | resp, err := http.DefaultClient.Do(req) |
| 66 | require.NoError(t, err) |
| 67 | defer resp.Body.Close() |
| 68 | |
| 69 | assert.Equal(t, http.StatusOK, resp.StatusCode) |
| 70 | assert.Equal(t, tt.expectedContentType, resp.Header.Get("Content-Type"), |
| 71 | "Content-Type header mismatch for %s", tt.name) |
| 72 | }) |
| 73 | } |
| 74 | } |