1616from warehouse .api import integrity
1717
1818
19- def test_select_content_type (db_request ):
20- db_request .accept = "application/json"
19+ @pytest .mark .parametrize (
20+ ("accept" , "expected" ),
21+ [
22+ # Simple cases
23+ (
24+ "application/vnd.pypi.integrity.v1+json" ,
25+ integrity .MIME_PYPI_INTEGRITY_V1_JSON ,
26+ ),
27+ ("application/json" , integrity .MIME_APPLICATION_JSON ),
28+ # No accept header means we give the user our first offer
29+ (None , integrity .MIME_PYPI_INTEGRITY_V1_JSON ),
30+ # Accept header contains only things we don't offer
31+ ("text/xml" , None ),
32+ ("application/octet-stream" , None ),
33+ ("text/xml, application/octet-stream" , None ),
34+ # Accept header contains both things we offer and things we don't;
35+ # we pick our matching offer even if the q-value is lower
36+ (
37+ "text/xml, application/vnd.pypi.integrity.v1+json" ,
38+ integrity .MIME_PYPI_INTEGRITY_V1_JSON ,
39+ ),
40+ (
41+ "application/vnd.pypi.integrity.v1+json; q=0.1, text/xml" ,
42+ integrity .MIME_PYPI_INTEGRITY_V1_JSON ,
43+ ),
44+ # Accept header contains multiple things we offer with the same q-value;
45+ # we pick our preferred offer
46+ (
47+ "application/json, application/vnd.pypi.integrity.v1+json" ,
48+ integrity .MIME_PYPI_INTEGRITY_V1_JSON ,
49+ ),
50+ (
51+ "application/vnd.pypi.integrity.v1+json; q=0.5, application/json; q=0.5" ,
52+ integrity .MIME_PYPI_INTEGRITY_V1_JSON ,
53+ ),
54+ # Accept header contains multiple things we offer; we pick our
55+ # offer based on the q-value
56+ (
57+ "application/vnd.pypi.integrity.v1+json; q=0.1, application/json" ,
58+ integrity .MIME_APPLICATION_JSON ,
59+ ),
60+ ],
61+ )
62+ def test_select_content_type (db_request , accept , expected ):
63+ db_request .accept = accept
2164
22- assert (
23- integrity ._select_content_type (db_request )
24- == integrity .MIME_PYPI_INTEGRITY_V1_JSON
25- )
65+ assert integrity ._select_content_type (db_request ) == expected
2666
2767
2868# Backstop; can be removed/changed once this view supports HTML.
2969@pytest .mark .parametrize (
3070 "content_type" ,
31- [integrity . MIME_TEXT_HTML , integrity .MIME_PYPI_INTEGRITY_V1_HTML ],
71+ ["text/html" , "application/vnd.pypi. integrity.v1+html" ],
3272)
3373def test_provenance_for_file_bad_accept (db_request , content_type ):
3474 db_request .accept = content_type
@@ -37,6 +77,15 @@ def test_provenance_for_file_bad_accept(db_request, content_type):
3777 assert response .json == {"message" : "Request not acceptable" }
3878
3979
80+ def test_provenance_for_file_accept_multiple (db_request , monkeypatch ):
81+ db_request .accept = "text/html, application/vnd.pypi.integrity.v1+json; q=0.9"
82+ file = pretend .stub (provenance = None , filename = "fake-1.2.3.tar.gz" )
83+
84+ response = integrity .provenance_for_file (file , db_request )
85+ assert response .status_code == 404
86+ assert response .json == {"message" : "No provenance available for fake-1.2.3.tar.gz" }
87+
88+
4089def test_provenance_for_file_not_enabled (db_request , monkeypatch ):
4190 monkeypatch .setattr (db_request , "flags" , pretend .stub (enabled = lambda * a : True ))
4291
0 commit comments