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

setting content type header for successful proxied requests #45521

Conversation

diogoasouza
Copy link
Contributor

@diogoasouza diogoasouza commented May 16, 2024

This PR uses mime.TypeByExtension to set the Content-Type header of proxied extension requests. If it fails to get the type, we use the one returned by the request.

@bigkevmcd
Copy link
Contributor

Is there a test we could add for this?

@diogoasouza
Copy link
Contributor Author

Is there a test we could add for this?

Maybe an integration test, I'll check it!

@diogoasouza
Copy link
Contributor Author

Added a test @bigkevmcd, can you review it?

Copy link
Member

@rohitsakala rohitsakala left a 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"))
Copy link
Contributor

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.

Copy link
Contributor

@bigkevmcd bigkevmcd May 20, 2024

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.

Copy link
Contributor Author

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

@diogoasouza
Copy link
Contributor Author

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 ?

The endpoint is https://localhost:8443/v1/uiplugins/top-level-product/0.1.0/<path_to_file>. Since you provided an empty path, 400 was expected in this case. You can try https://localhost:8443/v1/uiplugins/top-level-product/0.1.0/ + any entry form here https://github.com/rancher/ui-plugin-examples/blob/main/extensions/top-level-product/0.1.0/files.txt

@diogoasouza diogoasouza force-pushed the setting-content-type-header-for-extensions-without-cache branch from 0599e07 to fc4a73e Compare May 20, 2024 22:22
@diogoasouza diogoasouza force-pushed the setting-content-type-header-for-extensions-without-cache branch from fc4a73e to 00ff97e Compare May 20, 2024 22:24
}
}))
defer ts.Close()
denyFunc = func(string) bool { return false }
Copy link
Contributor

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 .

Copy link
Contributor Author

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

@diogoasouza diogoasouza force-pushed the setting-content-type-header-for-extensions-without-cache branch from 00ff97e to f58c562 Compare May 22, 2024 00:16
Copy link
Contributor

@bigkevmcd bigkevmcd left a 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.

@diogoasouza diogoasouza merged commit de5d190 into rancher:release/v2.9 May 22, 2024
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants