1+ import pytest
2+ from unittest import TestCase
3+ from osbot_utils .type_safe .primitives .domains .http .safe_str .Safe_Str__Http__Accept import Safe_Str__Http__Accept
4+
5+
6+ class test_Safe_Str__Http__Accept (TestCase ):
7+
8+ def test__init__ (self ): # Test Safe_Str__Http__Accept initialization
9+ accept = Safe_Str__Http__Accept ('application/json' )
10+ assert type (accept ) is Safe_Str__Http__Accept
11+ assert str (accept ) == 'application/json'
12+ assert accept == 'application/json'
13+
14+ def test__simple_mime_types (self ): # Test simple MIME type values
15+ assert Safe_Str__Http__Accept ('text/html' ) == 'text/html'
16+ assert Safe_Str__Http__Accept ('application/json' ) == 'application/json'
17+ assert Safe_Str__Http__Accept ('application/xml' ) == 'application/xml'
18+ assert Safe_Str__Http__Accept ('image/png' ) == 'image/png'
19+ assert Safe_Str__Http__Accept ('image/jpeg' ) == 'image/jpeg'
20+ assert Safe_Str__Http__Accept ('video/mp4' ) == 'video/mp4'
21+ assert Safe_Str__Http__Accept ('audio/mpeg' ) == 'audio/mpeg'
22+ assert Safe_Str__Http__Accept ('text/plain' ) == 'text/plain'
23+ assert Safe_Str__Http__Accept ('text/css' ) == 'text/css'
24+ assert Safe_Str__Http__Accept ('application/javascript' ) == 'application/javascript'
25+
26+ def test__wildcard_types (self ): # Test wildcard MIME types
27+ assert Safe_Str__Http__Accept ('*/*' ) == '*/*'
28+ assert Safe_Str__Http__Accept ('text/*' ) == 'text/*'
29+ assert Safe_Str__Http__Accept ('application/*' ) == 'application/*'
30+ assert Safe_Str__Http__Accept ('image/*' ) == 'image/*'
31+ assert Safe_Str__Http__Accept ('audio/*' ) == 'audio/*'
32+ assert Safe_Str__Http__Accept ('video/*' ) == 'video/*'
33+
34+ def test__quality_parameters (self ): # Test quality (q) parameter values
35+ assert Safe_Str__Http__Accept ('text/html;q=0.9' ) == 'text/html;q=0.9'
36+ assert Safe_Str__Http__Accept ('application/json;q=1.0' ) == 'application/json;q=1.0'
37+ assert Safe_Str__Http__Accept ('*/*;q=0.8' ) == '*/*;q=0.8'
38+ assert Safe_Str__Http__Accept ('text/plain;q=0.5' ) == 'text/plain;q=0.5'
39+ assert Safe_Str__Http__Accept ('image/webp;q=0.95' ) == 'image/webp;q=0.95'
40+
41+ def test__multiple_mime_types (self ): # Test multiple MIME types in one header
42+ assert Safe_Str__Http__Accept ('text/html,application/json' ) == 'text/html,application/json'
43+ assert Safe_Str__Http__Accept ('text/html, application/json' ) == 'text/html, application/json'
44+ assert Safe_Str__Http__Accept ('text/html,application/xml,application/json' ) == 'text/html,application/xml,application/json'
45+
46+ def test__complex_accept_headers (self ): # Test complex real-world Accept headers
47+ browser_accept = 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8'
48+ assert Safe_Str__Http__Accept (browser_accept ) == browser_accept
49+
50+ api_accept = 'application/json, text/plain, */*'
51+ assert Safe_Str__Http__Accept (api_accept ) == api_accept
52+
53+ chrome_accept = 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8'
54+ assert Safe_Str__Http__Accept (chrome_accept ) == chrome_accept
55+
56+ def test__with_charset_parameter (self ): # Test MIME types with charset parameter
57+ assert Safe_Str__Http__Accept ('text/html; charset=utf-8' ) == 'text/html; charset=utf-8'
58+ assert Safe_Str__Http__Accept ('application/json; charset=utf-8' ) == 'application/json; charset=utf-8'
59+ assert Safe_Str__Http__Accept ('text/plain; charset=iso-8859-1' ) == 'text/plain; charset=iso-8859-1'
60+
61+ def test__vendor_specific_mime_types (self ): # Test vendor-specific MIME types
62+ assert Safe_Str__Http__Accept ('application/vnd.api+json' ) == 'application/vnd.api+json'
63+ assert Safe_Str__Http__Accept ('application/vnd.ms-excel' ) == 'application/vnd.ms-excel'
64+ assert Safe_Str__Http__Accept ('application/ld+json' ) == 'application/ld+json'
65+ assert Safe_Str__Http__Accept ('application/vnd.github.v3+json' ) == 'application/vnd.github.v3+json'
66+
67+ def test__decimal_quality_values (self ): # Test various quality value decimal formats
68+ assert Safe_Str__Http__Accept ('text/html;q=1' ) == 'text/html;q=1'
69+ assert Safe_Str__Http__Accept ('text/html;q=0.9' ) == 'text/html;q=0.9'
70+ assert Safe_Str__Http__Accept ('text/html;q=0.99' ) == 'text/html;q=0.99'
71+ assert Safe_Str__Http__Accept ('text/html;q=0.999' ) == 'text/html;q=0.999'
72+ assert Safe_Str__Http__Accept ('text/html;q=0' ) == 'text/html;q=0'
73+
74+ def test__level_parameters (self ): # Test level parameter in Accept headers
75+ assert Safe_Str__Http__Accept ('text/html; level=1' ) == 'text/html; level=1'
76+ assert Safe_Str__Http__Accept ('text/html; level=2; q=0.9' ) == 'text/html; level=2; q=0.9'
77+
78+ def test__whitespace_handling (self ): # Test trim_whitespace = True
79+ assert Safe_Str__Http__Accept (' text/html ' ) == 'text/html'
80+ assert Safe_Str__Http__Accept ('application/json ' ) == 'application/json'
81+ assert Safe_Str__Http__Accept (' */*' ) == '*/*'
82+
83+ def test__numeric_conversion (self ): # Test conversion from numeric types
84+ assert Safe_Str__Http__Accept (12345 ) == '12345'
85+ assert Safe_Str__Http__Accept (999 ) == '999'
86+
87+ def test__invalid_characters (self ): # Test regex character replacement
88+ assert Safe_Str__Http__Accept ('text/html<script>' ) == 'text/html_script_'
89+ assert Safe_Str__Http__Accept ('text@html' ) == 'text_html'
90+ assert Safe_Str__Http__Accept ('text:html' ) == 'text_html'
91+ assert Safe_Str__Http__Accept ('application#json' ) == 'application_json'
92+
93+ def test__empty_values (self ): # Test allow_empty = True
94+ assert Safe_Str__Http__Accept (None ) == ''
95+ assert Safe_Str__Http__Accept ('' ) == ''
96+ assert Safe_Str__Http__Accept (' ' ) == '' # Spaces only (will be trimmed)
97+
98+ def test__max_length (self ): # Test TYPE_SAFE_STR__HTTP__ACCEPT__MAX_LENGTH = 512
99+ valid_512 = 'a' * 512
100+ invalid_513 = 'a' * 513
101+
102+ assert Safe_Str__Http__Accept (valid_512 ) == valid_512
103+
104+ with pytest .raises (ValueError ) as exc_info :
105+ Safe_Str__Http__Accept (invalid_513 )
106+ assert "in Safe_Str__Http__Accept, value exceeds maximum length of 512" in str (exc_info .value )
107+
108+ def test__special_subtypes (self ): # Test special MIME subtype formats
109+ assert Safe_Str__Http__Accept ('application/x-www-form-urlencoded' ) == 'application/x-www-form-urlencoded'
110+ assert Safe_Str__Http__Accept ('multipart/form-data' ) == 'multipart/form-data'
111+ assert Safe_Str__Http__Accept ('text/event-stream' ) == 'text/event-stream'
112+ assert Safe_Str__Http__Accept ('application/octet-stream' ) == 'application/octet-stream'
113+
114+ def test__image_formats (self ): # Test various image format MIME types
115+ assert Safe_Str__Http__Accept ('image/png' ) == 'image/png'
116+ assert Safe_Str__Http__Accept ('image/jpeg' ) == 'image/jpeg'
117+ assert Safe_Str__Http__Accept ('image/gif' ) == 'image/gif'
118+ assert Safe_Str__Http__Accept ('image/webp' ) == 'image/webp'
119+ assert Safe_Str__Http__Accept ('image/svg+xml' ) == 'image/svg+xml'
120+ assert Safe_Str__Http__Accept ('image/avif' ) == 'image/avif'
121+ assert Safe_Str__Http__Accept ('image/apng' ) == 'image/apng'
122+
123+ def test__str_and_repr (self ): # Test string representations
124+ accept = Safe_Str__Http__Accept ('application/json' )
125+
126+ assert str (accept ) == 'application/json'
127+ assert f"{ accept } " == 'application/json'
128+ assert f"Accept: { accept } " == 'Accept: application/json'
0 commit comments