|
| 1 | +package com.baeldung.http; |
| 2 | + |
| 3 | +import org.apache.commons.lang.StringUtils; |
| 4 | +import org.junit.Test; |
| 5 | +import static org.junit.Assert.*; |
| 6 | + |
| 7 | +import java.io.BufferedReader; |
| 8 | +import java.io.DataOutputStream; |
| 9 | +import java.io.IOException; |
| 10 | +import java.io.InputStreamReader; |
| 11 | +import java.net.CookieManager; |
| 12 | +import java.net.HttpCookie; |
| 13 | +import java.net.HttpURLConnection; |
| 14 | +import java.net.URL; |
| 15 | +import java.util.HashMap; |
| 16 | +import java.util.List; |
| 17 | +import java.util.Map; |
| 18 | +import java.util.Optional; |
| 19 | + |
| 20 | +public class HttpRequestTest { |
| 21 | + |
| 22 | + @Test |
| 23 | + public void whenGetRequest_thenOk() throws IOException { |
| 24 | + URL url = new URL("http://example.com"); |
| 25 | + HttpURLConnection con = (HttpURLConnection) url.openConnection(); |
| 26 | + con.setRequestMethod("GET"); |
| 27 | + |
| 28 | + Map<String, String> parameters = new HashMap<>(); |
| 29 | + parameters.put("param1", "val"); |
| 30 | + con.setDoOutput(true); |
| 31 | + DataOutputStream out = new DataOutputStream(con.getOutputStream()); |
| 32 | + out.writeBytes(ParameterStringBuilder.getParamsString(parameters)); |
| 33 | + out.flush(); |
| 34 | + out.close(); |
| 35 | + |
| 36 | + con.setConnectTimeout(5000); |
| 37 | + con.setReadTimeout(5000); |
| 38 | + |
| 39 | + int status = con.getResponseCode(); |
| 40 | + BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream())); |
| 41 | + String inputLine; |
| 42 | + StringBuffer content = new StringBuffer(); |
| 43 | + while ((inputLine = in.readLine()) != null) { |
| 44 | + content.append(inputLine); |
| 45 | + } |
| 46 | + in.close(); |
| 47 | + |
| 48 | + assertEquals("status code incorrect", status, 200); |
| 49 | + assertTrue("content incorrect", content.toString().contains("Example Domain")); |
| 50 | + } |
| 51 | + |
| 52 | + @Test |
| 53 | + public void whenPostRequest_thenOk() throws IOException { |
| 54 | + URL url = new URL("http://example.com"); |
| 55 | + HttpURLConnection con = (HttpURLConnection) url.openConnection(); |
| 56 | + con.setRequestMethod("POST"); |
| 57 | + con.setRequestProperty("Content-Type", "application/json"); |
| 58 | + |
| 59 | + Map<String, String> parameters = new HashMap<>(); |
| 60 | + parameters.put("param1", "val"); |
| 61 | + con.setDoOutput(true); |
| 62 | + DataOutputStream out = new DataOutputStream(con.getOutputStream()); |
| 63 | + out.writeBytes(ParameterStringBuilder.getParamsString(parameters)); |
| 64 | + out.flush(); |
| 65 | + out.close(); |
| 66 | + |
| 67 | + int status = con.getResponseCode(); |
| 68 | + BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream())); |
| 69 | + String inputLine; |
| 70 | + StringBuffer content = new StringBuffer(); |
| 71 | + while ((inputLine = in.readLine()) != null) { |
| 72 | + content.append(inputLine); |
| 73 | + } |
| 74 | + in.close(); |
| 75 | + |
| 76 | + assertEquals("status code incorrect", status, 200); |
| 77 | + } |
| 78 | + |
| 79 | + @Test |
| 80 | + public void whenGetCookies_thenOk() throws IOException { |
| 81 | + URL url = new URL("http://example.com"); |
| 82 | + HttpURLConnection con = (HttpURLConnection) url.openConnection(); |
| 83 | + con.setRequestMethod("GET"); |
| 84 | + |
| 85 | + CookieManager cookieManager = new CookieManager(); |
| 86 | + String cookiesHeader = con.getHeaderField("Set-Cookie"); |
| 87 | + Optional<HttpCookie> usernameCookie = null; |
| 88 | + if (cookiesHeader != null) { |
| 89 | + List<HttpCookie> cookies = HttpCookie.parse(cookiesHeader); |
| 90 | + cookies.forEach(cookie -> cookieManager.getCookieStore().add(null, cookie)); |
| 91 | + usernameCookie = cookies.stream().findAny().filter(cookie -> cookie.getName().equals("username")); |
| 92 | + } |
| 93 | + |
| 94 | + if (usernameCookie == null) { |
| 95 | + cookieManager.getCookieStore().add(null, new HttpCookie("username", "john")); |
| 96 | + } |
| 97 | + |
| 98 | + con.disconnect(); |
| 99 | + |
| 100 | + con = (HttpURLConnection) url.openConnection(); |
| 101 | + con.setRequestProperty("Cookie", StringUtils.join(cookieManager.getCookieStore().getCookies(), ";")); |
| 102 | + |
| 103 | + int status = con.getResponseCode(); |
| 104 | + |
| 105 | + assertEquals("status code incorrect", status, 200); |
| 106 | + } |
| 107 | + |
| 108 | + @Test |
| 109 | + public void whenRedirect_thenOk() throws IOException { |
| 110 | + URL url = new URL("http://example.com"); |
| 111 | + HttpURLConnection con = (HttpURLConnection) url.openConnection(); |
| 112 | + con.setRequestMethod("GET"); |
| 113 | + |
| 114 | + con.setInstanceFollowRedirects(true); |
| 115 | + int status = con.getResponseCode(); |
| 116 | + |
| 117 | + if (status == HttpURLConnection.HTTP_MOVED_TEMP || status == HttpURLConnection.HTTP_MOVED_PERM) { |
| 118 | + String location = con.getHeaderField("Location"); |
| 119 | + URL newUrl = new URL(location); |
| 120 | + con = (HttpURLConnection) newUrl.openConnection(); |
| 121 | + } |
| 122 | + |
| 123 | + assertEquals("status code incorrect", con.getResponseCode(), 200); |
| 124 | + } |
| 125 | + |
| 126 | +} |
0 commit comments