77import java .io .DataOutputStream ;
88import java .io .IOException ;
99import java .io .InputStreamReader ;
10+ import java .io .Reader ;
1011import java .net .CookieManager ;
1112import java .net .HttpCookie ;
1213import java .net .HttpURLConnection ;
@@ -48,7 +49,8 @@ public void whenGetRequest_thenOk() throws IOException {
4849 in .close ();
4950
5051 assertEquals ("status code incorrect" , status , 200 );
51- assertTrue ("content incorrect" , content .toString ().contains ("Example Domain" ));
52+ assertTrue ("content incorrect" , content .toString ()
53+ .contains ("Example Domain" ));
5254 }
5355
5456 @ Test
@@ -89,18 +91,24 @@ public void whenGetCookies_thenOk() throws IOException {
8991 Optional <HttpCookie > usernameCookie = null ;
9092 if (cookiesHeader != null ) {
9193 List <HttpCookie > cookies = HttpCookie .parse (cookiesHeader );
92- cookies .forEach (cookie -> cookieManager .getCookieStore ().add (null , cookie ));
93- usernameCookie = cookies .stream ().findAny ().filter (cookie -> cookie .getName ().equals ("username" ));
94+ cookies .forEach (cookie -> cookieManager .getCookieStore ()
95+ .add (null , cookie ));
96+ usernameCookie = cookies .stream ()
97+ .findAny ()
98+ .filter (cookie -> cookie .getName ()
99+ .equals ("username" ));
94100 }
95101
96102 if (usernameCookie == null ) {
97- cookieManager .getCookieStore ().add (null , new HttpCookie ("username" , "john" ));
103+ cookieManager .getCookieStore ()
104+ .add (null , new HttpCookie ("username" , "john" ));
98105 }
99106
100107 con .disconnect ();
101108
102109 con = (HttpURLConnection ) url .openConnection ();
103- con .setRequestProperty ("Cookie" , StringUtils .join (cookieManager .getCookieStore ().getCookies (), ";" ));
110+ con .setRequestProperty ("Cookie" , StringUtils .join (cookieManager .getCookieStore ()
111+ .getCookies (), ";" ));
104112
105113 int status = con .getResponseCode ();
106114
@@ -125,4 +133,56 @@ public void whenRedirect_thenOk() throws IOException {
125133 assertEquals ("status code incorrect" , con .getResponseCode (), 200 );
126134 }
127135
136+ @ Test
137+ public void whenFailedRequest_thenOk () throws IOException {
138+ URL url = new URL ("http://example.com" );
139+ HttpURLConnection con = (HttpURLConnection ) url .openConnection ();
140+ con .setRequestMethod ("POST" );
141+
142+ con .setConnectTimeout (5000 );
143+ con .setReadTimeout (5000 );
144+
145+ int status = con .getResponseCode ();
146+
147+ Reader streamReader = null ;
148+
149+ if (status > 299 ) {
150+ streamReader = new InputStreamReader (con .getErrorStream ());
151+ } else {
152+ streamReader = new InputStreamReader (con .getInputStream ());
153+ }
154+
155+ BufferedReader in = new BufferedReader (streamReader );
156+ String inputLine ;
157+ StringBuilder content = new StringBuilder ();
158+ while ((inputLine = in .readLine ()) != null ) {
159+ content .append (inputLine );
160+ }
161+ in .close ();
162+
163+ con .disconnect ();
164+
165+ assertEquals ("status code incorrect" , status , 411 );
166+ assertTrue ("error content" , content .toString ()
167+ .contains ("411 - Length Required" ));
168+ }
169+
170+ @ Test
171+ public void whenGetRequestFullResponse_thenOk () throws IOException {
172+ URL url = new URL ("http://example.com" );
173+ HttpURLConnection con = (HttpURLConnection ) url .openConnection ();
174+ con .setRequestMethod ("GET" );
175+
176+ con .setConnectTimeout (5000 );
177+ con .setReadTimeout (5000 );
178+
179+ String fullResponse = FullResponseBuilder .getFullResponse (con );
180+
181+ con .disconnect ();
182+
183+ assertEquals ("status code incorrect" , con .getResponseCode (), 200 );
184+ assertTrue ("header incorrect" , fullResponse .contains ("Content-Type: text/html; charset=UTF-8" ));
185+ assertTrue ("response incorrect" , fullResponse .contains ("<!doctype html><html><head>" ));
186+ }
187+
128188}
0 commit comments