<s> import unittest <EOL> from flask import Flask <EOL> from flask_negotiate import consumes , produces <EOL> HTML = '<STR_LIT>' <EOL> JSON = '<STR_LIT:application/json>' <EOL> XML = '<STR_LIT>' <EOL> UNSUPPORTED_CODE = <NUM_LIT> <EOL> NOT_ACCEPTABLE_CODE = <NUM_LIT> <EOL> CONSUMES_JSON_ONLY = '<STR_LIT>' <EOL> CONSUMES_JSON_AND_HTML = '<STR_LIT>' <EOL> PRODUCES_JSON_ONLY = '<STR_LIT>' <EOL> PRODUCES_JSON_AND_HTML = '<STR_LIT>' <EOL> HTML_HEADERS = { '<STR_LIT>' : HTML } <EOL> JSON_HEADERS = { '<STR_LIT>' : JSON } <EOL> XML_HEADERS = { '<STR_LIT>' : XML } <EOL> class NegotiateTestCase ( unittest . TestCase ) : <EOL> def setUp ( self ) : <EOL> app = Flask ( __name__ ) <EOL> app . config [ '<STR_LIT>' ] = True <EOL> @ app . route ( CONSUMES_JSON_ONLY ) <EOL> @ consumes ( JSON ) <EOL> def consumes_json_only ( ) : <EOL> return CONSUMES_JSON_ONLY <EOL> @ app . route ( CONSUMES_JSON_AND_HTML ) <EOL> @ consumes ( JSON , HTML ) <EOL> def consumes_json_and_html ( ) : <EOL> return CONSUMES_JSON_AND_HTML <EOL> @ app . route ( PRODUCES_JSON_ONLY ) <EOL> @ produces ( JSON ) <EOL> def produces_json_only ( ) : <EOL> return PRODUCES_JSON_ONLY <EOL> @ app . route ( PRODUCES_JSON_AND_HTML ) <EOL> @ produces ( JSON , HTML ) <EOL> def produces_json_and_html ( ) : <EOL> return PRODUCES_JSON_AND_HTML <EOL> self . app = app <EOL> self . client = app . test_client ( ) <EOL> def assertUnsupported ( self , r ) : <EOL> self . assertEqual ( r . status_code , UNSUPPORTED_CODE ) <EOL> def assertUnacceptable ( self , r ) : <EOL> self . assertEqual ( r . status_code , NOT_ACCEPTABLE_CODE ) <EOL> def test_consumes_json_only_valid_content ( self ) : <EOL> r = self . client . get ( CONSUMES_JSON_ONLY , content_type = JSON ) <EOL> self . assertIn ( CONSUMES_JSON_ONLY , r . data ) <EOL> def test_consumes_json_only_invalid_content ( self ) : <EOL> r = self . client . get ( CONSUMES_JSON_ONLY , content_type = HTML ) <EOL> self . assertUnsupported ( r ) <EOL> def test_consumes_json_and_html_valid_content ( self ) : <EOL> for content_type in [ JSON , HTML ] : <EOL> r = self . client . get ( CONSUMES_JSON_AND_HTML , <EOL> content_type = content_type ) <EOL> self . assertIn ( CONSUMES_JSON_AND_HTML , r . data ) <EOL> def test_consumes_json_and_html_invalid_content ( self ) : <EOL> r = self . client . get ( CONSUMES_JSON_AND_HTML , content_type = XML ) <EOL> self . assertUnsupported ( r ) <EOL> def test_produces_json_only_valid_accept ( self ) : <EOL> r = self . client . get ( PRODUCES_JSON_ONLY , headers = JSON_HEADERS ) <EOL> self . assertIn ( PRODUCES_JSON_ONLY , r . data ) <EOL> def test_produces_json_only_invalid_accept ( self ) : <EOL> r = self . client . get ( PRODUCES_JSON_ONLY , headers = HTML_HEADERS ) <EOL> self . assertUnacceptable ( r ) <EOL> def test_produces_json_and_html_valid_accept ( self ) : <EOL> for headers in [ JSON_HEADERS , HTML_HEADERS ] : <EOL> r = self . client . get ( PRODUCES_JSON_AND_HTML , headers = headers ) <EOL> self . assertIn ( PRODUCES_JSON_AND_HTML , r . data ) <EOL> def test_produces_json_and_html_ivalid_accept ( self ) : <EOL> r = self . client . get ( PRODUCES_JSON_AND_HTML , headers = XML_HEADERS ) <EOL> self . assertUnacceptable ( r ) </s>
