1+ package com .cbfacademy ;
2+
3+ import static org .junit .jupiter .api .Assertions .assertEquals ;
4+ import static org .junit .jupiter .api .Assertions .assertFalse ;
5+ import static org .junit .jupiter .api .Assertions .assertNotEquals ;
6+ import static org .junit .jupiter .api .Assertions .assertNotNull ;
7+ import static org .junit .jupiter .api .Assertions .assertNotSame ;
8+ import static org .junit .jupiter .api .Assertions .assertTrue ;
9+
10+ import java .net .URI ;
11+ import java .net .http .HttpClient ;
12+ import java .net .http .HttpRequest ;
13+ import java .time .Duration ;
14+ import java .util .Optional ;
15+
16+ import org .junit .jupiter .api .DisplayName ;
17+ import org .junit .jupiter .api .Test ;
18+ import org .junit .jupiter .params .ParameterizedTest ;
19+ import org .junit .jupiter .params .provider .ValueSource ;
20+
21+ @ DisplayName ("HttpRequestBuilder Tests" )
22+ public class HttpRequestBuilderTest {
23+
24+ @ Test
25+ @ DisplayName ("Should build HttpRequest with correct method" )
26+ void shouldBuildRequestWithGetMethod () {
27+ String testUrl = "https://example.com" ;
28+ HttpRequest request = HttpRequestBuilder .build (testUrl );
29+
30+ assertEquals ("GET" , request .method (), "Request method should be GET" );
31+ }
32+
33+ @ Test
34+ @ DisplayName ("Should build HttpRequest with correct URI" )
35+ void shouldBuildRequestWithCorrectUri () {
36+ String testUrl = "https://example.com/api/data" ;
37+ HttpRequest request = HttpRequestBuilder .build (testUrl );
38+
39+ assertEquals (URI .create (testUrl ), request .uri (), "Request URI should match the provided URL" );
40+ }
41+
42+ @ Test
43+ @ DisplayName ("Should build HttpRequest with HTTP_1_1 version" )
44+ void shouldBuildRequestWithHttp11Version () {
45+ String testUrl = "https://example.com" ;
46+ HttpRequest request = HttpRequestBuilder .build (testUrl );
47+
48+ Optional <HttpClient .Version > version = request .version ();
49+ assertTrue (version .isPresent (), "HTTP version should be present" );
50+ assertEquals (HttpClient .Version .HTTP_1_1 , version .get (), "HTTP version should be HTTP_1_1" );
51+ }
52+
53+ @ Test
54+ @ DisplayName ("Should build HttpRequest with correct User-Agent header" )
55+ void shouldBuildRequestWithCorrectUserAgent () {
56+ String testUrl = "https://example.com" ;
57+ HttpRequest request = HttpRequestBuilder .build (testUrl );
58+
59+ Optional <String > userAgent = request .headers ().firstValue ("User-Agent" );
60+ assertTrue (userAgent .isPresent (), "User-Agent header should be present" );
61+ assertEquals ("Mozilla/5.0 (Java Exercise Client)" , userAgent .get (),
62+ "User-Agent should match expected value" );
63+ }
64+
65+ @ Test
66+ @ DisplayName ("Should build HttpRequest with correct Accept header" )
67+ void shouldBuildRequestWithCorrectAcceptHeader () {
68+ String testUrl = "https://example.com" ;
69+ HttpRequest request = HttpRequestBuilder .build (testUrl );
70+
71+ Optional <String > accept = request .headers ().firstValue ("Accept" );
72+ assertTrue (accept .isPresent (), "Accept header should be present" );
73+ assertEquals ("text/html,application/json,*/*;q=0.8" , accept .get (),
74+ "Accept header should match expected value" );
75+ }
76+
77+ @ Test
78+ @ DisplayName ("Should build HttpRequest with 30 second timeout" )
79+ void shouldBuildRequestWithCorrectTimeout () {
80+ String testUrl = "https://example.com" ;
81+ HttpRequest request = HttpRequestBuilder .build (testUrl );
82+
83+ Optional <Duration > timeout = request .timeout ();
84+ assertTrue (timeout .isPresent (), "Timeout should be present" );
85+ assertEquals (Duration .ofSeconds (30 ), timeout .get (), "Timeout should be 30 seconds" );
86+ }
87+
88+ @ ParameterizedTest
89+ @ ValueSource (strings = {
90+ "https://www.google.com" ,
91+ "https://api.github.com/users" ,
92+ "https://httpbin.org/get" ,
93+ "https://example.com/path/to/resource?param=value"
94+ })
95+ @ DisplayName ("Should handle various URL formats correctly" )
96+ void shouldHandleVariousUrlFormats (String url ) {
97+ HttpRequest request = HttpRequestBuilder .build (url );
98+
99+ assertNotNull (request , "Request should not be null" );
100+ assertEquals (URI .create (url ), request .uri (), "Request URI should match input URL" );
101+ assertEquals ("GET" , request .method (), "Method should always be GET" );
102+ }
103+
104+ @ Test
105+ @ DisplayName ("Should build request with all required headers present" )
106+ void shouldBuildRequestWithAllRequiredHeaders () {
107+ String testUrl = "https://example.com" ;
108+ HttpRequest request = HttpRequestBuilder .build (testUrl );
109+
110+ // Verify that both required headers are present
111+ assertTrue (request .headers ().firstValue ("User-Agent" ).isPresent (),
112+ "User-Agent header should be present" );
113+ assertTrue (request .headers ().firstValue ("Accept" ).isPresent (),
114+ "Accept header should be present" );
115+
116+ // Verify no restricted headers are set (Connection header should not be manually set)
117+ assertFalse (request .headers ().firstValue ("Connection" ).isPresent (),
118+ "Connection header should not be manually set" );
119+ }
120+
121+ @ Test
122+ @ DisplayName ("Should create different request instances for different URLs" )
123+ void shouldCreateDifferentRequestInstances () {
124+ String url1 = "https://example.com/path1" ;
125+ String url2 = "https://example.com/path2" ;
126+
127+ HttpRequest request1 = HttpRequestBuilder .build (url1 );
128+ HttpRequest request2 = HttpRequestBuilder .build (url2 );
129+
130+ assertNotSame (request1 , request2 , "Different requests should be separate instances" );
131+ assertNotEquals (request1 .uri (), request2 .uri (), "Different requests should have different URIs" );
132+ }
133+ }
0 commit comments