Skip to content

Commit

Permalink
fix: cors expose swarm-tag header for bzz dir upload (#2450)
Browse files Browse the repository at this point in the history
  • Loading branch information
AuHau authored Aug 31, 2021
1 parent 787e66f commit 5b25866
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 9 deletions.
32 changes: 23 additions & 9 deletions pkg/api/bzz_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ func TestBzzFiles(t *testing.T) {
},
})
address := swarm.MustParseHexAddress("f30c0aa7e9e2a0ef4c9b1b750ebfeaeb7c7c24da700bb089da19a46e3677824b")
jsonhttptest.Request(t, client, http.MethodPost, fileUploadResource, http.StatusCreated,
rcvdHeader := jsonhttptest.Request(t, client, http.MethodPost, fileUploadResource, http.StatusCreated,
jsonhttptest.WithRequestHeader(api.SwarmPostageBatchIdHeader, batchOkStr),
jsonhttptest.WithRequestBody(tr),
jsonhttptest.WithRequestHeader("Content-Type", api.ContentTypeTar),
Expand All @@ -100,6 +100,8 @@ func TestBzzFiles(t *testing.T) {
}),
)

isTagFoundInResponse(t, rcvdHeader, nil)

has, err := storerMock.Has(context.Background(), address)
if err != nil {
t.Fatal(err)
Expand Down Expand Up @@ -145,7 +147,7 @@ func TestBzzFiles(t *testing.T) {
},
})
reference := swarm.MustParseHexAddress("f30c0aa7e9e2a0ef4c9b1b750ebfeaeb7c7c24da700bb089da19a46e3677824b")
jsonhttptest.Request(t, client, http.MethodPost, fileUploadResource, http.StatusCreated,
rcvdHeader := jsonhttptest.Request(t, client, http.MethodPost, fileUploadResource, http.StatusCreated,
jsonhttptest.WithRequestHeader(api.SwarmPostageBatchIdHeader, batchOkStr),
jsonhttptest.WithRequestHeader(api.SwarmPinHeader, "true"),
jsonhttptest.WithRequestBody(tr),
Expand All @@ -155,6 +157,8 @@ func TestBzzFiles(t *testing.T) {
}),
)

isTagFoundInResponse(t, rcvdHeader, nil)

has, err := storerMock.Has(context.Background(), reference)
if err != nil {
t.Fatal(err)
Expand All @@ -179,7 +183,7 @@ func TestBzzFiles(t *testing.T) {
fileName := "my-pictures.jpeg"

var resp api.BzzUploadResponse
jsonhttptest.Request(t, client, http.MethodPost,
rcvdHeader := jsonhttptest.Request(t, client, http.MethodPost,
fileUploadResource+"?name="+fileName, http.StatusCreated,
jsonhttptest.WithRequestHeader(api.SwarmPostageBatchIdHeader, batchOkStr),
jsonhttptest.WithRequestBody(bytes.NewReader(simpleData)),
Expand All @@ -188,8 +192,10 @@ func TestBzzFiles(t *testing.T) {
jsonhttptest.WithUnmarshalJSONResponse(&resp),
)

isTagFoundInResponse(t, rcvdHeader, nil)

rootHash := resp.Reference.String()
rcvdHeader := jsonhttptest.Request(t, client, http.MethodGet,
rcvdHeader = jsonhttptest.Request(t, client, http.MethodGet,
fileDownloadResource(rootHash), http.StatusOK,
jsonhttptest.WithExpectedResponse(simpleData),
)
Expand All @@ -210,7 +216,7 @@ func TestBzzFiles(t *testing.T) {
fileName := "my-pictures.jpeg"
rootHash := "4f9146b3813ccbd7ce45a18be23763d7e436ab7a3982ef39961c6f3cd4da1dcf"

jsonhttptest.Request(t, client, http.MethodPost,
rcvdHeader := jsonhttptest.Request(t, client, http.MethodPost,
fileUploadResource+"?name="+fileName, http.StatusCreated,
jsonhttptest.WithRequestHeader(api.SwarmPostageBatchIdHeader, batchOkStr),
jsonhttptest.WithRequestBody(bytes.NewReader(simpleData)),
Expand All @@ -220,7 +226,9 @@ func TestBzzFiles(t *testing.T) {
jsonhttptest.WithRequestHeader("Content-Type", "image/jpeg; charset=utf-8"),
)

rcvdHeader := jsonhttptest.Request(t, client, http.MethodGet,
isTagFoundInResponse(t, rcvdHeader, nil)

rcvdHeader = jsonhttptest.Request(t, client, http.MethodGet,
fileDownloadResource(rootHash), http.StatusOK,
jsonhttptest.WithExpectedResponse(simpleData),
)
Expand Down Expand Up @@ -265,6 +273,8 @@ func TestBzzFiles(t *testing.T) {
t.Fatal("Invalid ETags header received")
}

isTagFoundInResponse(t, rcvdHeader, nil)

// try to fetch the same file and check the data
rcvdHeader = jsonhttptest.Request(t, client, http.MethodGet,
fileDownloadResource(rootHash), http.StatusOK,
Expand All @@ -290,7 +300,7 @@ func TestBzzFiles(t *testing.T) {
fileName := "simple_file.txt"
rootHash := "65148cd89b58e91616773f5acea433f7b5a6274f2259e25f4893a332b74a7e28"

jsonhttptest.Request(t, client, http.MethodPost,
rcvdHeader := jsonhttptest.Request(t, client, http.MethodPost,
fileUploadResource+"?name="+fileName, http.StatusCreated,
jsonhttptest.WithRequestHeader(api.SwarmPostageBatchIdHeader, batchOkStr),
jsonhttptest.WithRequestBody(bytes.NewReader(simpleData)),
Expand All @@ -300,7 +310,9 @@ func TestBzzFiles(t *testing.T) {
jsonhttptest.WithRequestHeader("Content-Type", "text/html; charset=utf-8"),
)

rcvdHeader := jsonhttptest.Request(t, client, http.MethodGet,
isTagFoundInResponse(t, rcvdHeader, nil)

rcvdHeader = jsonhttptest.Request(t, client, http.MethodGet,
fileDownloadResource(rootHash)+"?targets="+targets, http.StatusOK,
jsonhttptest.WithExpectedResponse(simpleData),
)
Expand Down Expand Up @@ -420,10 +432,12 @@ func TestBzzFilesRangeRequests(t *testing.T) {
testOpts = append(testOpts, jsonhttptest.WithRequestHeader(api.SwarmCollectionHeader, "True"))
}

jsonhttptest.Request(t, client, http.MethodPost, upload.uploadEndpoint, http.StatusCreated,
rcvdHeader := jsonhttptest.Request(t, client, http.MethodPost, upload.uploadEndpoint, http.StatusCreated,
testOpts...,
)

isTagFoundInResponse(t, rcvdHeader, nil)

var downloadPath string
if upload.downloadEndpoint != "/bytes" {
downloadPath = upload.downloadEndpoint + "/" + resp.Reference.String() + "/" + upload.filepath
Expand Down
1 change: 1 addition & 0 deletions pkg/api/dirs.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ func (s *server) dirUploadHandler(w http.ResponseWriter, r *http.Request, storer
}
}

w.Header().Set("Access-Control-Expose-Headers", SwarmTagHeader)
w.Header().Set(SwarmTagHeader, fmt.Sprint(tag.Uid))
jsonhttp.Created(w, bzzUploadResponse{
Reference: reference,
Expand Down

0 comments on commit 5b25866

Please sign in to comment.