-
Notifications
You must be signed in to change notification settings - Fork 2.9k
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
setting content type header for successful proxied requests #45521
setting content type header for successful proxied requests #45521
Conversation
Is there a test we could add for this? |
Maybe an integration test, I'll check it! |
Added a test @bigkevmcd, can you review it? |
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 was trying to test this but I am getting an error.
I installed top-level-product
and set nocache to true and then I tried hitting this url https://localhost:8443/v1/uiplugins/top-level-product/0.1.0/
where I get 400: Invalid request
Am I doing something wrong here in testing this ?
@@ -108,6 +110,26 @@ func (w *UIPluginTest) SetupSuite() { | |||
}, "extensions-examples")) |
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.
This is good, but you could test the code in the catalog
package itself.
func TestProxyRequest_content_type(t *testing.T) {
response := "testing:\n value: test\n"
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Disposition", "attachment; filename=testing.yaml")
if _, err := w.Write([]byte(response)); err != nil {
t.Fatal(err)
}
}))
defer ts.Close()
req := httptest.NewRequest(http.MethodGet, "http://example.com", nil)
w := httptest.NewRecorder()
proxyRequest(ts.URL, "/testing.yaml", w, req, func(string) bool { return false })
resp := w.Result()
body, _ := io.ReadAll(resp.Body)
if resp.StatusCode != http.StatusOK {
t.Errorf("got StatusCode %v, want %v", resp.StatusCode, http.StatusOK)
}
wantContent := "application/yaml"
if ct := resp.Header.Get("Content-Type"); ct != wantContent {
t.Errorf("got Content-Type %s, want %s", ct, wantContent)
}
if string(body) != response {
t.Errorf("read body: %s", body)
}
}
Note that I had to modify proxyRequest
to accept a denied func(string) bool
parameter and pass in denyList
and then provide one that allows localhost
addresses to be able to proxy through the test server.
But, you could redesign the UIPluginHandler
as a struct which contained the denied addresses and had the index handler and pluginHandler
as methods which would deal with this.
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.
High-level integration tests tell you that something isn't working, lower-level tests can usually tell you what isn't working.
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 added this test. I changed the file to javascript though, since the mime
package and http.DetectContentType
both don't recognize yaml files
The endpoint is |
0599e07
to
fc4a73e
Compare
fc4a73e
to
00ff97e
Compare
pkg/api/steve/catalog/plugin_test.go
Outdated
} | ||
})) | ||
defer ts.Close() | ||
denyFunc = func(string) bool { return false } |
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.
You need to store the old one, and put it back, otherwise any additional tests would have undefined behaviour.
This is why passing in the verifier is a better option .
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.
That's true, I updated the code
00ff97e
to
f58c562
Compare
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.
Additional tests should really be added for cases where the mime-type doesn't match, but this is definitely better than it was.
This PR uses
mime.TypeByExtension
to set theContent-Type
header of proxied extension requests. If it fails to get the type, we use the one returned by the request.