-
Notifications
You must be signed in to change notification settings - Fork 25
[VC-43403] Various improvements to the mock API server #690
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -38,20 +38,17 @@ func (mds *mockDataUploadServer) Close() { | |
func (mds *mockDataUploadServer) ServeHTTP(w http.ResponseWriter, r *http.Request) { | ||
switch r.URL.Path { | ||
case apiPathSnapshotLinks: | ||
mds.handlePresignedUpload(w, r) | ||
mds.handleSnapshotLinks(w, r) | ||
return | ||
case "/presigned-upload": | ||
mds.handleUpload(w, r, false) | ||
return | ||
case "/presigned-upload-invalid-json": | ||
mds.handleUpload(w, r, false) | ||
mds.handlePresignedUpload(w, r) | ||
return | ||
default: | ||
w.WriteHeader(http.StatusNotFound) | ||
} | ||
} | ||
|
||
func (mds *mockDataUploadServer) handlePresignedUpload(w http.ResponseWriter, r *http.Request) { | ||
func (mds *mockDataUploadServer) handleSnapshotLinks(w http.ResponseWriter, r *http.Request) { | ||
if r.Method != http.MethodPost { | ||
w.WriteHeader(http.StatusMethodNotAllowed) | ||
_, _ = w.Write([]byte(`{"message":"method not allowed"}`)) | ||
|
@@ -120,9 +117,15 @@ func (mds *mockDataUploadServer) handlePresignedUpload(w http.ResponseWriter, r | |
|
||
// An example of a real checksum mismatch error from the AWS API when the | ||
// request body does not match the checksum in the request header. | ||
const amzExampleChecksumError = `<Error><Code>BadDigest</Code><Message>The SHA256 you specified did not match the calculated checksum.</Message><RequestId>GBDMP09BEZ929YBK</RequestId><HostId>sFTQb9JQpfJY/t+Ctn0anBmp4lKzEGES8ttmfAmFInuJIhvaV/U+20vYaGbdtlEnExZQRV/5xo6RQqq3xItM+px/Q2AEiv1G</HostId></Error>` | ||
const amzExampleChecksumError = `<?xml version="1.0" encoding="UTF-8"?> | ||
<Error> | ||
<Code>BadDigest</Code> | ||
<Message>The SHA256 you specified did not match the calculated checksum.</Message> | ||
<RequestId>THR2V1RX700Z8SC7</RequestId> | ||
<HostId>F0xSC0H93Xs0BlCx6RjasZgrtjNkNB7lF4+yz1AiPQHswpdEoqj3iTgEN8SUWgV2Qm/laPobVIMz9SYTNHqdoA==</HostId> | ||
</Error>` | ||
|
||
func (mds *mockDataUploadServer) handleUpload(w http.ResponseWriter, r *http.Request, invalidJSON bool) { | ||
func (mds *mockDataUploadServer) handlePresignedUpload(w http.ResponseWriter, r *http.Request) { | ||
if r.Method != http.MethodPut { | ||
w.WriteHeader(http.StatusMethodNotAllowed) | ||
_, _ = w.Write([]byte(`{"message":"method not allowed"}`)) | ||
|
@@ -134,13 +137,6 @@ func (mds *mockDataUploadServer) handleUpload(w http.ResponseWriter, r *http.Req | |
return | ||
} | ||
|
||
if invalidJSON { | ||
w.WriteHeader(http.StatusOK) | ||
w.Header().Set("Content-Type", "application/json") | ||
_, _ = w.Write([]byte(`{"url":`)) // invalid JSON | ||
return | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This wasn't used. I think it's a legacy of some previous version of the mock server. |
||
|
||
amzChecksum := r.Header.Get("X-Amz-Checksum-Sha256") | ||
if amzChecksum == "" { | ||
http.Error(w, "should set x-amz-checksum-sha256 header on all requests", http.StatusInternalServerError) | ||
|
@@ -150,11 +146,12 @@ func (mds *mockDataUploadServer) handleUpload(w http.ResponseWriter, r *http.Req | |
checksum := sha256.New() | ||
_, _ = io.Copy(checksum, r.Body) | ||
|
||
// AWS S3 responds with a BadDigest error if the request body has a | ||
// different checksum than the checksum supplied in the request header. | ||
if amzChecksum != base64.StdEncoding.EncodeToString(checksum.Sum(nil)) { | ||
w.Header().Set("Content-Type", "application/xml") | ||
http.Error(w, amzExampleChecksumError, http.StatusBadRequest) | ||
} | ||
|
||
// AWS S3 responds with an empty body if the PUT succeeds | ||
w.WriteHeader(http.StatusOK) | ||
w.Header().Set("Content-Type", "application/json") | ||
_, _ = w.Write([]byte(`{"success":true}`)) | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I temporarily modified the client to send mismatching body and request header digest, and this is the error recorded by mitmproxy:
