Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: cors expose swarm-tag header for bzz dir upload #2450

Merged
merged 2 commits into from
Aug 31, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
8 changes: 4 additions & 4 deletions pkg/api/tag_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ func TestTags(t *testing.T) {
jsonhttptest.WithRequestHeader(api.SwarmTagHeader, strconv.FormatUint(uint64(tr.Uid), 10)),
)

isTagFoundInResponse(t, rcvdHeaders, &tr)
IsTagFoundInResponse(t, rcvdHeaders, &tr)
tagValueTest(t, tr.Uid, 1, 1, 1, 0, 0, 0, swarm.ZeroAddress, client)
})

Expand Down Expand Up @@ -396,7 +396,7 @@ func TestTags(t *testing.T) {
}),
jsonhttptest.WithRequestHeader(api.SwarmTagHeader, strconv.FormatUint(uint64(tr.Uid), 10)),
)
id := isTagFoundInResponse(t, rcvdHeaders, nil)
id := IsTagFoundInResponse(t, rcvdHeaders, nil)

tagToVerify, err := tag.Get(id)
if err != nil {
Expand All @@ -410,9 +410,9 @@ func TestTags(t *testing.T) {
})
}

// isTagFoundInResponse verifies that the tag id is found in the supplied HTTP headers
// IsTagFoundInResponse verifies that the tag id is found in the supplied HTTP headers
// if an API tag response is supplied, it also verifies that it contains an id which matches the headers
func isTagFoundInResponse(t *testing.T, headers http.Header, tr *api.TagResponse) uint32 {
func IsTagFoundInResponse(t *testing.T, headers http.Header, tr *api.TagResponse) uint32 {
AuHau marked this conversation as resolved.
Show resolved Hide resolved
t.Helper()

idStr := headers.Get(api.SwarmTagHeader)
Expand Down