public static ContentType getContentType(String header) { final String[] arr = header.split("\\s*;\\s*"); if(arr.length == 1) { return new ContentTypeBean(header, null); } else { final String contentType = arr[0]; for(int i=1; i<arr.length; i++) { final String headerPart = arr[i]; if(headerPart.contains("charset=")) { Pattern p = Pattern.compile("charset=(.+)"); Matcher m = p.matcher(headerPart); if(m.matches()) { Charset charset = Charset.forName(m.group(1)); return new ContentTypeBean(contentType, charset); } } } return new ContentTypeBean(contentType, null); } }
private static ContentType getContentType(Element e) { String contentType = e.getAttributeValue("content-type"); String charsetStr = e.getAttributeValue("charset"); if(StringUtil.isNotEmpty(contentType)) { return new ContentTypeBean(contentType, (charsetStr!=null? Charset.forName(charsetStr): null)); } else { return null; } } }
@Override public ContentType getContentType() { if(StringUtil.isNotEmpty(jd.getContentType())) { return new ContentTypeBean(jd.getContentType(), jd.getCharset()); } else { return null; } }
/** * Test of getContentType method, of class HttpUtil. */ @Test public void testGetContentType_String() { System.out.println("getContentType"); String header = "application/vnd.mnet.staticwebspaces+xml;version=1;charset=UTF-8"; ContentType expResult = new ContentTypeBean("application/vnd.mnet.staticwebspaces+xml", Charsets.UTF_8); ContentType result = HttpUtil.getContentType(header); assertEquals(expResult, result); } }
@Test public void testFileBody() throws Exception { System.out.println("testFileBody"); RequestBean expResult = getRequestBeanWithoutBody(); ContentType ct = new ContentTypeBean("text/plain", Charsets.UTF_8); ReqEntityFileBean body = new ReqEntityFileBean(new File("/etc/hosts"), ct); expResult.setBody(body); Request actual = p.getRequestFromFile(new File("src/test/resources/reqBodyFile.rcq")); assertEquals(expResult, actual); }
@Test public void testMultipartBody() throws Exception { System.out.println("testMultipartBody"); List<ReqEntityPart> parts = new ArrayList<>(); ContentType ct = new ContentTypeBean("text/plain", Charsets.UTF_8); ReqEntityFilePartBean partFile = new ReqEntityFilePartBean("hosts-txt-name", "hosts.txt", ct, new File("/etc/hosts")); ReqEntityStringPartBean partString = new ReqEntityStringPartBean("aarthi.txt", ct, "Hello Babes!"); parts.add(partFile); parts.add(partString); ReqEntityMultipartBean expResult = new ReqEntityMultipartBean(parts, null, MultipartSubtype.FORM_DATA); Request actual = p.getRequestFromFile(new File("src/test/resources/reqBodyMultipart.rcq")); assertEquals(expResult, actual.getBody()); } }
@Test public void testStringBody() throws Exception { System.out.println("testStringBody"); RequestBean expResult = getRequestBeanWithoutBody(); ContentType ct = new ContentTypeBean("text/plain", Charsets.UTF_8); ReqEntityStringBean body = new ReqEntityStringBean("Subhash loves Aarthi", ct); expResult.setBody(body); Request actual = p.getRequestFromFile(new File("src/test/resources/reqBodyString.rcq")); assertEquals(expResult, actual); }
@Test public void testByteArrayBody() throws Exception { System.out.println("testByteArrayBody"); RequestBean expResult = getRequestBeanWithoutBody(); ContentType ct = new ContentTypeBean("image/jpeg", null); byte[] arr = FileUtil.getContentAsBytes(new File("src/test/resources/subhash_by_aarthi.jpeg")); ReqEntityByteArrayBean body = new ReqEntityByteArrayBean(arr, ct); expResult.setBody(body); Request actual = p.getRequestFromFile(new File("src/test/resources/reqBodyByteArray.rcq")); assertEquals(expResult, actual); }
System.out.println("run"); final ContentType contentType = new ContentTypeBean("test/text", Charsets.UTF_8); RequestBean request = getRequestBean(); request.setMethod(HTTPMethod.POST);
private RequestBean getDefaultRequestBean() throws MalformedURLException{ RequestBean expResult = new RequestBean(); expResult.setUrl(new URL("http://localhost:10101/")); expResult.setMethod(HTTPMethod.POST); expResult.addHeader("key1", "value1"); ContentType contentType = new ContentTypeBean("text/plain", Charsets.UTF_8); expResult.setBody(new ReqEntityStringBean("Body Text", contentType)); BasicAuthBean auth = new BasicAuthBean(); auth.setPreemptive(true); auth.setRealm("realm"); auth.setUsername("username"); auth.setPassword("password".toCharArray()); expResult.setAuth(auth); expResult.setFollowRedirect(true); return expResult; }