Advertisement
Guest User

Untitled

a guest
May 16th, 2017
611
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 109.51 KB | None | 0 0
  1. /*
  2. * Copyright (c) 2014 Kevin Sawicki <kevinsawicki@gmail.com>
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy
  5. * of this software and associated documentation files (the "Software"), to
  6. * deal in the Software without restriction, including without limitation the
  7. * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. * sell copies of the Software, and to permit persons to whom the Software is
  9. * furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. * IN THE SOFTWARE.
  21. */
  22. package com.github.kevinsawicki.http;
  23.  
  24. import static com.github.kevinsawicki.http.HttpRequest.CHARSET_UTF8;
  25. import static com.github.kevinsawicki.http.HttpRequest.delete;
  26. import static com.github.kevinsawicki.http.HttpRequest.encode;
  27. import static com.github.kevinsawicki.http.HttpRequest.get;
  28. import static com.github.kevinsawicki.http.HttpRequest.head;
  29. import static com.github.kevinsawicki.http.HttpRequest.options;
  30. import static com.github.kevinsawicki.http.HttpRequest.post;
  31. import static com.github.kevinsawicki.http.HttpRequest.put;
  32. import static com.github.kevinsawicki.http.HttpRequest.trace;
  33. import static java.net.HttpURLConnection.HTTP_BAD_REQUEST;
  34. import static java.net.HttpURLConnection.HTTP_CREATED;
  35. import static java.net.HttpURLConnection.HTTP_INTERNAL_ERROR;
  36. import static java.net.HttpURLConnection.HTTP_NO_CONTENT;
  37. import static java.net.HttpURLConnection.HTTP_NOT_MODIFIED;
  38. import static java.net.HttpURLConnection.HTTP_OK;
  39. import static org.junit.Assert.assertEquals;
  40. import static org.junit.Assert.assertFalse;
  41. import static org.junit.Assert.assertNotNull;
  42. import static org.junit.Assert.assertNull;
  43. import static org.junit.Assert.assertTrue;
  44. import static org.junit.Assert.fail;
  45. import com.github.kevinsawicki.http.HttpRequest.HttpRequestException;
  46. import com.github.kevinsawicki.http.HttpRequest.ConnectionFactory;
  47. import com.github.kevinsawicki.http.HttpRequest.UploadProgress;
  48.  
  49. import java.io.BufferedReader;
  50. import java.io.ByteArrayInputStream;
  51. import java.io.ByteArrayOutputStream;
  52. import java.io.File;
  53. import java.io.FileInputStream;
  54. import java.io.FileReader;
  55. import java.io.FileWriter;
  56. import java.io.IOException;
  57. import java.io.InputStream;
  58. import java.io.PrintStream;
  59. import java.io.StringWriter;
  60. import java.io.UnsupportedEncodingException;
  61. import java.io.Writer;
  62. import java.net.HttpURLConnection;
  63. import java.net.Proxy;
  64. import java.net.URL;
  65. import java.util.Arrays;
  66. import java.util.Collections;
  67. import java.util.HashMap;
  68. import java.util.LinkedHashMap;
  69. import java.util.List;
  70. import java.util.Map;
  71. import java.util.Map.Entry;
  72. import java.util.concurrent.atomic.AtomicBoolean;
  73. import java.util.concurrent.atomic.AtomicInteger;
  74. import java.util.concurrent.atomic.AtomicLong;
  75. import java.util.concurrent.atomic.AtomicReference;
  76. import java.util.zip.GZIPOutputStream;
  77.  
  78. import javax.net.ssl.HttpsURLConnection;
  79. import javax.servlet.ServletException;
  80. import javax.servlet.http.HttpServletRequest;
  81. import javax.servlet.http.HttpServletResponse;
  82.  
  83. import org.eclipse.jetty.server.Request;
  84. import org.eclipse.jetty.util.B64Code;
  85. import org.junit.After;
  86. import org.junit.BeforeClass;
  87. import org.junit.Test;
  88.  
  89. /**
  90. * Unit tests of {@link HttpRequest}
  91. */
  92. public class HttpRequestTest extends ServerTestCase {
  93.  
  94. private static String url;
  95.  
  96. private static RequestHandler handler;
  97.  
  98. /**
  99. * Set up server
  100. *
  101. * @throws Exception
  102. */
  103. @BeforeClass
  104. public static void startServer() throws Exception {
  105. url = setUp(new RequestHandler() {
  106.  
  107. @Override
  108. public void handle(String target, Request baseRequest,
  109. HttpServletRequest request, HttpServletResponse response)
  110. throws IOException, ServletException {
  111. if (handler != null)
  112. handler.handle(target, baseRequest, request, response);
  113. }
  114.  
  115. @Override
  116. public void handle(Request request, HttpServletResponse response) {
  117. if (handler != null)
  118. handler.handle(request, response);
  119. }
  120. });
  121. }
  122.  
  123. /**
  124. * Clear handler
  125. */
  126. @After
  127. public void clearHandler() {
  128. handler = null;
  129. }
  130.  
  131. /**
  132. * Create request with malformed URL
  133. */
  134. @Test(expected = HttpRequestException.class)
  135. public void malformedStringUrl() {
  136. get("\\m/");
  137. }
  138.  
  139. /**
  140. * Create request with malformed URL
  141. */
  142. @Test
  143. public void malformedStringUrlCause() {
  144. try {
  145. delete("\\m/");
  146. fail("Exception not thrown");
  147. } catch (HttpRequestException e) {
  148. assertNotNull(e.getCause());
  149. }
  150. }
  151.  
  152. /**
  153. * Set request buffer size to negative value
  154. */
  155. @Test(expected = IllegalArgumentException.class)
  156. public void negativeBufferSize() {
  157. get("http://localhost").bufferSize(-1);
  158. }
  159.  
  160. /**
  161. * Make a GET request with an empty body response
  162. *
  163. * @throws Exception
  164. */
  165. @Test
  166. public void getEmpty() throws Exception {
  167. final AtomicReference<String> method = new AtomicReference<String>();
  168. handler = new RequestHandler() {
  169.  
  170. @Override
  171. public void handle(Request request, HttpServletResponse response) {
  172. method.set(request.getMethod());
  173. response.setStatus(HTTP_OK);
  174. }
  175. };
  176. HttpRequest request = get(url);
  177. assertNotNull(request.getConnection());
  178. assertEquals(30000, request.readTimeout(30000).getConnection()
  179. .getReadTimeout());
  180. assertEquals(50000, request.connectTimeout(50000).getConnection()
  181. .getConnectTimeout());
  182. assertEquals(2500, request.bufferSize(2500).bufferSize());
  183. assertFalse(request.ignoreCloseExceptions(false).ignoreCloseExceptions());
  184. assertFalse(request.useCaches(false).getConnection().getUseCaches());
  185. int code = request.code();
  186. assertTrue(request.ok());
  187. assertFalse(request.created());
  188. assertFalse(request.badRequest());
  189. assertFalse(request.serverError());
  190. assertFalse(request.notFound());
  191. assertFalse(request.notModified());
  192. assertEquals("GET", method.get());
  193. assertEquals("OK", request.message());
  194. assertEquals(HTTP_OK, code);
  195. assertEquals("", request.body());
  196. assertNotNull(request.toString());
  197. assertFalse(request.toString().length() == 0);
  198. assertEquals(request, request.disconnect());
  199. assertTrue(request.isBodyEmpty());
  200. assertEquals(request.url().toString(), url);
  201. assertEquals("GET", request.method());
  202. }
  203.  
  204. /**
  205. * Make a GET request with an empty body response
  206. *
  207. * @throws Exception
  208. */
  209. @Test
  210. public void getUrlEmpty() throws Exception {
  211. final AtomicReference<String> method = new AtomicReference<String>();
  212. handler = new RequestHandler() {
  213.  
  214. @Override
  215. public void handle(Request request, HttpServletResponse response) {
  216. method.set(request.getMethod());
  217. response.setStatus(HTTP_OK);
  218. }
  219. };
  220. HttpRequest request = get(new URL(url));
  221. assertNotNull(request.getConnection());
  222. int code = request.code();
  223. assertTrue(request.ok());
  224. assertFalse(request.created());
  225. assertFalse(request.noContent());
  226. assertFalse(request.badRequest());
  227. assertFalse(request.serverError());
  228. assertFalse(request.notFound());
  229. assertEquals("GET", method.get());
  230. assertEquals("OK", request.message());
  231. assertEquals(HTTP_OK, code);
  232. assertEquals("", request.body());
  233. }
  234.  
  235. /**
  236. * Make a GET request with an empty body response
  237. *
  238. * @throws Exception
  239. */
  240. @Test
  241. public void getNoContent() throws Exception {
  242. final AtomicReference<String> method = new AtomicReference<String>();
  243. handler = new RequestHandler() {
  244.  
  245. @Override
  246. public void handle(Request request, HttpServletResponse response) {
  247. method.set(request.getMethod());
  248. response.setStatus(HTTP_NO_CONTENT);
  249. }
  250. };
  251. HttpRequest request = get(new URL(url));
  252. assertNotNull(request.getConnection());
  253. int code = request.code();
  254. assertFalse(request.ok());
  255. assertFalse(request.created());
  256. assertTrue(request.noContent());
  257. assertFalse(request.badRequest());
  258. assertFalse(request.serverError());
  259. assertFalse(request.notFound());
  260. assertEquals("GET", method.get());
  261. assertEquals("No Content", request.message());
  262. assertEquals(HTTP_NO_CONTENT, code);
  263. assertEquals("", request.body());
  264. }
  265.  
  266. /**
  267. * Make a GET request with a URL that needs encoding
  268. *
  269. * @throws Exception
  270. */
  271. @Test
  272. public void getUrlEncodedWithSpace() throws Exception {
  273. String unencoded = "/a resource";
  274. final AtomicReference<String> path = new AtomicReference<String>();
  275. handler = new RequestHandler() {
  276.  
  277. @Override
  278. public void handle(Request request, HttpServletResponse response) {
  279. path.set(request.getPathInfo());
  280. response.setStatus(HTTP_OK);
  281. }
  282. };
  283. HttpRequest request = get(encode(url + unencoded));
  284. assertTrue(request.ok());
  285. assertEquals(unencoded, path.get());
  286. }
  287.  
  288. /**
  289. * Make a GET request with a URL that needs encoding
  290. *
  291. * @throws Exception
  292. */
  293. @Test
  294. public void getUrlEncodedWithUnicode() throws Exception {
  295. String unencoded = "/\u00DF";
  296. final AtomicReference<String> path = new AtomicReference<String>();
  297. handler = new RequestHandler() {
  298.  
  299. @Override
  300. public void handle(Request request, HttpServletResponse response) {
  301. path.set(request.getPathInfo());
  302. response.setStatus(HTTP_OK);
  303. }
  304. };
  305. HttpRequest request = get(encode(url + unencoded));
  306. assertTrue(request.ok());
  307. assertEquals(unencoded, path.get());
  308. }
  309.  
  310. /**
  311. * Make a GET request with a URL that needs encoding
  312. *
  313. * @throws Exception
  314. */
  315. @Test
  316. public void getUrlEncodedWithPercent() throws Exception {
  317. String unencoded = "/%";
  318. final AtomicReference<String> path = new AtomicReference<String>();
  319. handler = new RequestHandler() {
  320.  
  321. @Override
  322. public void handle(Request request, HttpServletResponse response) {
  323. path.set(request.getPathInfo());
  324. response.setStatus(HTTP_OK);
  325. }
  326. };
  327. HttpRequest request = get(encode(url + unencoded));
  328. assertTrue(request.ok());
  329. assertEquals(unencoded, path.get());
  330. }
  331.  
  332. /**
  333. * Make a DELETE request with an empty body response
  334. *
  335. * @throws Exception
  336. */
  337. @Test
  338. public void deleteEmpty() throws Exception {
  339. final AtomicReference<String> method = new AtomicReference<String>();
  340. handler = new RequestHandler() {
  341.  
  342. @Override
  343. public void handle(Request request, HttpServletResponse response) {
  344. method.set(request.getMethod());
  345. response.setStatus(HTTP_OK);
  346. }
  347. };
  348. HttpRequest request = delete(url);
  349. assertNotNull(request.getConnection());
  350. assertTrue(request.ok());
  351. assertFalse(request.notFound());
  352. assertEquals("DELETE", method.get());
  353. assertEquals("", request.body());
  354. assertEquals("DELETE", request.method());
  355. }
  356.  
  357. /**
  358. * Make a DELETE request with an empty body response
  359. *
  360. * @throws Exception
  361. */
  362. @Test
  363. public void deleteUrlEmpty() throws Exception {
  364. final AtomicReference<String> method = new AtomicReference<String>();
  365. handler = new RequestHandler() {
  366.  
  367. @Override
  368. public void handle(Request request, HttpServletResponse response) {
  369. method.set(request.getMethod());
  370. response.setStatus(HTTP_OK);
  371. }
  372. };
  373. HttpRequest request = delete(new URL(url));
  374. assertNotNull(request.getConnection());
  375. assertTrue(request.ok());
  376. assertFalse(request.notFound());
  377. assertEquals("DELETE", method.get());
  378. assertEquals("", request.body());
  379. }
  380.  
  381. /**
  382. * Make an OPTIONS request with an empty body response
  383. *
  384. * @throws Exception
  385. */
  386. @Test
  387. public void optionsEmpty() throws Exception {
  388. final AtomicReference<String> method = new AtomicReference<String>();
  389. handler = new RequestHandler() {
  390.  
  391. @Override
  392. public void handle(Request request, HttpServletResponse response) {
  393. method.set(request.getMethod());
  394. response.setStatus(HTTP_OK);
  395. }
  396. };
  397. HttpRequest request = options(url);
  398. assertNotNull(request.getConnection());
  399. assertTrue(request.ok());
  400. assertFalse(request.notFound());
  401. assertEquals("OPTIONS", method.get());
  402. assertEquals("", request.body());
  403. }
  404.  
  405. /**
  406. * Make an OPTIONS request with an empty body response
  407. *
  408. * @throws Exception
  409. */
  410. @Test
  411. public void optionsUrlEmpty() throws Exception {
  412. final AtomicReference<String> method = new AtomicReference<String>();
  413. handler = new RequestHandler() {
  414.  
  415. @Override
  416. public void handle(Request request, HttpServletResponse response) {
  417. method.set(request.getMethod());
  418. response.setStatus(HTTP_OK);
  419. }
  420. };
  421. HttpRequest request = options(new URL(url));
  422. assertNotNull(request.getConnection());
  423. assertTrue(request.ok());
  424. assertFalse(request.notFound());
  425. assertEquals("OPTIONS", method.get());
  426. assertEquals("", request.body());
  427. }
  428.  
  429. /**
  430. * Make a HEAD request with an empty body response
  431. *
  432. * @throws Exception
  433. */
  434. @Test
  435. public void headEmpty() throws Exception {
  436. final AtomicReference<String> method = new AtomicReference<String>();
  437. handler = new RequestHandler() {
  438.  
  439. @Override
  440. public void handle(Request request, HttpServletResponse response) {
  441. method.set(request.getMethod());
  442. response.setStatus(HTTP_OK);
  443. }
  444. };
  445. HttpRequest request = head(url);
  446. assertNotNull(request.getConnection());
  447. assertTrue(request.ok());
  448. assertFalse(request.notFound());
  449. assertEquals("HEAD", method.get());
  450. assertEquals("", request.body());
  451. }
  452.  
  453. /**
  454. * Make a HEAD request with an empty body response
  455. *
  456. * @throws Exception
  457. */
  458. @Test
  459. public void headUrlEmpty() throws Exception {
  460. final AtomicReference<String> method = new AtomicReference<String>();
  461. handler = new RequestHandler() {
  462.  
  463. @Override
  464. public void handle(Request request, HttpServletResponse response) {
  465. method.set(request.getMethod());
  466. response.setStatus(HTTP_OK);
  467. }
  468. };
  469. HttpRequest request = head(new URL(url));
  470. assertNotNull(request.getConnection());
  471. assertTrue(request.ok());
  472. assertFalse(request.notFound());
  473. assertEquals("HEAD", method.get());
  474. assertEquals("", request.body());
  475. }
  476.  
  477. /**
  478. * Make a PUT request with an empty body response
  479. *
  480. * @throws Exception
  481. */
  482. @Test
  483. public void putEmpty() throws Exception {
  484. final AtomicReference<String> method = new AtomicReference<String>();
  485. handler = new RequestHandler() {
  486.  
  487. @Override
  488. public void handle(Request request, HttpServletResponse response) {
  489. method.set(request.getMethod());
  490. response.setStatus(HTTP_OK);
  491. }
  492. };
  493. HttpRequest request = put(url);
  494. assertNotNull(request.getConnection());
  495. assertTrue(request.ok());
  496. assertFalse(request.notFound());
  497. assertEquals("PUT", method.get());
  498. assertEquals("", request.body());
  499. }
  500.  
  501. /**
  502. * Make a PUT request with an empty body response
  503. *
  504. * @throws Exception
  505. */
  506. @Test
  507. public void putUrlEmpty() throws Exception {
  508. final AtomicReference<String> method = new AtomicReference<String>();
  509. handler = new RequestHandler() {
  510.  
  511. @Override
  512. public void handle(Request request, HttpServletResponse response) {
  513. method.set(request.getMethod());
  514. response.setStatus(HTTP_OK);
  515. }
  516. };
  517. HttpRequest request = put(new URL(url));
  518. assertNotNull(request.getConnection());
  519. assertTrue(request.ok());
  520. assertFalse(request.notFound());
  521. assertEquals("PUT", method.get());
  522. assertEquals("", request.body());
  523. }
  524.  
  525. /**
  526. * Make a PUT request with an empty body response
  527. *
  528. * @throws Exception
  529. */
  530. @Test
  531. public void traceEmpty() throws Exception {
  532. final AtomicReference<String> method = new AtomicReference<String>();
  533. handler = new RequestHandler() {
  534.  
  535. @Override
  536. public void handle(Request request, HttpServletResponse response) {
  537. method.set(request.getMethod());
  538. response.setStatus(HTTP_OK);
  539. }
  540. };
  541. HttpRequest request = trace(url);
  542. assertNotNull(request.getConnection());
  543. assertTrue(request.ok());
  544. assertFalse(request.notFound());
  545. assertEquals("TRACE", method.get());
  546. assertEquals("", request.body());
  547. }
  548.  
  549. /**
  550. * Make a TRACE request with an empty body response
  551. *
  552. * @throws Exception
  553. */
  554. @Test
  555. public void traceUrlEmpty() throws Exception {
  556. final AtomicReference<String> method = new AtomicReference<String>();
  557. handler = new RequestHandler() {
  558.  
  559. @Override
  560. public void handle(Request request, HttpServletResponse response) {
  561. method.set(request.getMethod());
  562. response.setStatus(HTTP_OK);
  563. }
  564. };
  565. HttpRequest request = trace(new URL(url));
  566. assertNotNull(request.getConnection());
  567. assertTrue(request.ok());
  568. assertFalse(request.notFound());
  569. assertEquals("TRACE", method.get());
  570. assertEquals("", request.body());
  571. }
  572.  
  573. /**
  574. * Make a POST request with an empty request body
  575. *
  576. * @throws Exception
  577. */
  578. @Test
  579. public void postEmpty() throws Exception {
  580. final AtomicReference<String> method = new AtomicReference<String>();
  581. handler = new RequestHandler() {
  582.  
  583. @Override
  584. public void handle(Request request, HttpServletResponse response) {
  585. method.set(request.getMethod());
  586. response.setStatus(HTTP_CREATED);
  587. }
  588. };
  589. HttpRequest request = post(url);
  590. int code = request.code();
  591. assertEquals("POST", method.get());
  592. assertFalse(request.ok());
  593. assertTrue(request.created());
  594. assertEquals(HTTP_CREATED, code);
  595. }
  596.  
  597. /**
  598. * Make a POST request with an empty request body
  599. *
  600. * @throws Exception
  601. */
  602. @Test
  603. public void postUrlEmpty() throws Exception {
  604. final AtomicReference<String> method = new AtomicReference<String>();
  605. handler = new RequestHandler() {
  606.  
  607. @Override
  608. public void handle(Request request, HttpServletResponse response) {
  609. method.set(request.getMethod());
  610. response.setStatus(HTTP_CREATED);
  611. }
  612. };
  613. HttpRequest request = post(new URL(url));
  614. int code = request.code();
  615. assertEquals("POST", method.get());
  616. assertFalse(request.ok());
  617. assertTrue(request.created());
  618. assertEquals(HTTP_CREATED, code);
  619. }
  620.  
  621. /**
  622. * Make a POST request with a non-empty request body
  623. *
  624. * @throws Exception
  625. */
  626. @Test
  627. public void postNonEmptyString() throws Exception {
  628. final AtomicReference<String> body = new AtomicReference<String>();
  629. handler = new RequestHandler() {
  630.  
  631. @Override
  632. public void handle(Request request, HttpServletResponse response) {
  633. body.set(new String(read()));
  634. response.setStatus(HTTP_OK);
  635. }
  636. };
  637. int code = post(url).send("hello").code();
  638. assertEquals(HTTP_OK, code);
  639. assertEquals("hello", body.get());
  640. }
  641.  
  642. /**
  643. * Make a POST request with a non-empty request body
  644. *
  645. * @throws Exception
  646. */
  647. @Test
  648. public void postNonEmptyFile() throws Exception {
  649. final AtomicReference<String> body = new AtomicReference<String>();
  650. handler = new RequestHandler() {
  651.  
  652. @Override
  653. public void handle(Request request, HttpServletResponse response) {
  654. body.set(new String(read()));
  655. response.setStatus(HTTP_OK);
  656. }
  657. };
  658. File file = File.createTempFile("post", ".txt");
  659. new FileWriter(file).append("hello").close();
  660. int code = post(url).send(file).code();
  661. assertEquals(HTTP_OK, code);
  662. assertEquals("hello", body.get());
  663. }
  664.  
  665. /**
  666. * Make a POST request with multiple files in the body
  667. *
  668. * @throws Exception
  669. */
  670. @Test
  671. public void postMultipleFiles() throws Exception {
  672. final AtomicReference<String> body = new AtomicReference<String>();
  673. handler = new RequestHandler() {
  674.  
  675. @Override
  676. public void handle(Request request, HttpServletResponse response) {
  677. body.set(new String(read()));
  678. response.setStatus(HTTP_OK);
  679. }
  680. };
  681.  
  682. File file1 = File.createTempFile("post", ".txt");
  683. new FileWriter(file1).append("hello").close();
  684.  
  685. File file2 = File.createTempFile("post", ".txt");
  686. new FileWriter(file2).append(" world").close();
  687.  
  688. int code = post(url).send(file1).send(file2).code();
  689. assertEquals(HTTP_OK, code);
  690. assertEquals("hello world", body.get());
  691. }
  692.  
  693. /**
  694. * Make a POST request with a non-empty request body
  695. *
  696. * @throws Exception
  697. */
  698. @Test
  699. public void postNonEmptyReader() throws Exception {
  700. final AtomicReference<String> body = new AtomicReference<String>();
  701. handler = new RequestHandler() {
  702.  
  703. @Override
  704. public void handle(Request request, HttpServletResponse response) {
  705. body.set(new String(read()));
  706. response.setStatus(HTTP_OK);
  707. }
  708. };
  709. File file = File.createTempFile("post", ".txt");
  710. new FileWriter(file).append("hello").close();
  711. int code = post(url).send(new FileReader(file)).code();
  712. assertEquals(HTTP_OK, code);
  713. assertEquals("hello", body.get());
  714. }
  715.  
  716. /**
  717. * Make a POST request with a non-empty request body
  718. *
  719. * @throws Exception
  720. */
  721. @Test
  722. public void postNonEmptyByteArray() throws Exception {
  723. final AtomicReference<String> body = new AtomicReference<String>();
  724. handler = new RequestHandler() {
  725.  
  726. @Override
  727. public void handle(Request request, HttpServletResponse response) {
  728. body.set(new String(read()));
  729. response.setStatus(HTTP_OK);
  730. }
  731. };
  732. byte[] bytes = "hello".getBytes(CHARSET_UTF8);
  733. int code = post(url).contentLength(Integer.toString(bytes.length))
  734. .send(bytes).code();
  735. assertEquals(HTTP_OK, code);
  736. assertEquals("hello", body.get());
  737. }
  738.  
  739. /**
  740. * Make a post with an explicit set of the content length
  741. *
  742. * @throws Exception
  743. */
  744. @Test
  745. public void postWithLength() throws Exception {
  746. final AtomicReference<String> body = new AtomicReference<String>();
  747. final AtomicReference<Integer> length = new AtomicReference<Integer>();
  748. handler = new RequestHandler() {
  749.  
  750. @Override
  751. public void handle(Request request, HttpServletResponse response) {
  752. body.set(new String(read()));
  753. length.set(request.getContentLength());
  754. response.setStatus(HTTP_OK);
  755. }
  756. };
  757. String data = "hello";
  758. int sent = data.getBytes().length;
  759. int code = post(url).contentLength(sent).send(data).code();
  760. assertEquals(HTTP_OK, code);
  761. assertEquals(sent, length.get().intValue());
  762. assertEquals(data, body.get());
  763. }
  764.  
  765. /**
  766. * Make a post of form data
  767. *
  768. * @throws Exception
  769. */
  770. @Test
  771. public void postForm() throws Exception {
  772. final AtomicReference<String> body = new AtomicReference<String>();
  773. final AtomicReference<String> contentType = new AtomicReference<String>();
  774. handler = new RequestHandler() {
  775.  
  776. @Override
  777. public void handle(Request request, HttpServletResponse response) {
  778. body.set(new String(read()));
  779. contentType.set(request.getContentType());
  780. response.setStatus(HTTP_OK);
  781. }
  782. };
  783. Map<String, String> data = new LinkedHashMap<String, String>();
  784. data.put("name", "user");
  785. data.put("number", "100");
  786. int code = post(url).form(data).form("zip", "12345").code();
  787. assertEquals(HTTP_OK, code);
  788. assertEquals("name=user&number=100&zip=12345", body.get());
  789. assertEquals("application/x-www-form-urlencoded; charset=UTF-8",
  790. contentType.get());
  791. }
  792.  
  793. /**
  794. * Make a post of form data
  795. *
  796. * @throws Exception
  797. */
  798. @Test
  799. public void postFormWithNoCharset() throws Exception {
  800. final AtomicReference<String> body = new AtomicReference<String>();
  801. final AtomicReference<String> contentType = new AtomicReference<String>();
  802. handler = new RequestHandler() {
  803.  
  804. @Override
  805. public void handle(Request request, HttpServletResponse response) {
  806. body.set(new String(read()));
  807. contentType.set(request.getContentType());
  808. response.setStatus(HTTP_OK);
  809. }
  810. };
  811. Map<String, String> data = new LinkedHashMap<String, String>();
  812. data.put("name", "user");
  813. data.put("number", "100");
  814. int code = post(url).form(data, null).form("zip", "12345").code();
  815. assertEquals(HTTP_OK, code);
  816. assertEquals("name=user&number=100&zip=12345", body.get());
  817. assertEquals("application/x-www-form-urlencoded", contentType.get());
  818. }
  819.  
  820. /**
  821. * Make a post with an empty form data map
  822. *
  823. * @throws Exception
  824. */
  825. @Test
  826. public void postEmptyForm() throws Exception {
  827. final AtomicReference<String> body = new AtomicReference<String>();
  828. handler = new RequestHandler() {
  829.  
  830. @Override
  831. public void handle(Request request, HttpServletResponse response) {
  832. body.set(new String(read()));
  833. response.setStatus(HTTP_OK);
  834. }
  835. };
  836. int code = post(url).form(new HashMap<String, String>()).code();
  837. assertEquals(HTTP_OK, code);
  838. assertEquals("", body.get());
  839. }
  840.  
  841. /**
  842. * Make a post in chunked mode
  843. *
  844. * @throws Exception
  845. */
  846. @Test
  847. public void chunkPost() throws Exception {
  848. final AtomicReference<String> body = new AtomicReference<String>();
  849. final AtomicReference<String> encoding = new AtomicReference<String>();
  850. handler = new RequestHandler() {
  851.  
  852. @Override
  853. public void handle(Request request, HttpServletResponse response) {
  854. body.set(new String(read()));
  855. response.setStatus(HTTP_OK);
  856. encoding.set(request.getHeader("Transfer-Encoding"));
  857. }
  858. };
  859. String data = "hello";
  860. int code = post(url).chunk(2).send(data).code();
  861. assertEquals(HTTP_OK, code);
  862. assertEquals(data, body.get());
  863. assertEquals("chunked", encoding.get());
  864. }
  865.  
  866. /**
  867. * Make a GET request for a non-empty response body
  868. *
  869. * @throws Exception
  870. */
  871. @Test
  872. public void getNonEmptyString() throws Exception {
  873. handler = new RequestHandler() {
  874.  
  875. @Override
  876. public void handle(Request request, HttpServletResponse response) {
  877. response.setStatus(HTTP_OK);
  878. write("hello");
  879. }
  880. };
  881. HttpRequest request = get(url);
  882. assertEquals(HTTP_OK, request.code());
  883. assertEquals("hello", request.body());
  884. assertEquals("hello".getBytes().length, request.contentLength());
  885. assertFalse(request.isBodyEmpty());
  886. }
  887.  
  888. /**
  889. * Make a GET request with a response that includes a charset parameter
  890. *
  891. * @throws Exception
  892. */
  893. @Test
  894. public void getWithResponseCharset() throws Exception {
  895. handler = new RequestHandler() {
  896.  
  897. @Override
  898. public void handle(Request request, HttpServletResponse response) {
  899. response.setStatus(HTTP_OK);
  900. response.setContentType("text/html; charset=UTF-8");
  901. }
  902. };
  903. HttpRequest request = get(url);
  904. assertEquals(HTTP_OK, request.code());
  905. assertEquals(CHARSET_UTF8, request.charset());
  906. }
  907.  
  908. /**
  909. * Make a GET request with a response that includes a charset parameter
  910. *
  911. * @throws Exception
  912. */
  913. @Test
  914. public void getWithResponseCharsetAsSecondParam() throws Exception {
  915. handler = new RequestHandler() {
  916.  
  917. @Override
  918. public void handle(Request request, HttpServletResponse response) {
  919. response.setStatus(HTTP_OK);
  920. response.setContentType("text/html; param1=val1; charset=UTF-8");
  921. }
  922. };
  923. HttpRequest request = get(url);
  924. assertEquals(HTTP_OK, request.code());
  925. assertEquals(CHARSET_UTF8, request.charset());
  926. }
  927.  
  928. /**
  929. * Make a GET request with basic authentication specified
  930. *
  931. * @throws Exception
  932. */
  933. @Test
  934. public void basicAuthentication() throws Exception {
  935. final AtomicReference<String> user = new AtomicReference<String>();
  936. final AtomicReference<String> password = new AtomicReference<String>();
  937. handler = new RequestHandler() {
  938.  
  939. @Override
  940. public void handle(Request request, HttpServletResponse response) {
  941. String auth = request.getHeader("Authorization");
  942. auth = auth.substring(auth.indexOf(' ') + 1);
  943. try {
  944. auth = B64Code.decode(auth, CHARSET_UTF8);
  945. } catch (UnsupportedEncodingException e) {
  946. throw new RuntimeException(e);
  947. }
  948. int colon = auth.indexOf(':');
  949. user.set(auth.substring(0, colon));
  950. password.set(auth.substring(colon + 1));
  951. response.setStatus(HTTP_OK);
  952. }
  953. };
  954. assertTrue(get(url).basic("user", "p4ssw0rd").ok());
  955. assertEquals("user", user.get());
  956. assertEquals("p4ssw0rd", password.get());
  957. }
  958.  
  959. /**
  960. * Make a GET request with basic proxy authentication specified
  961. *
  962. * @throws Exception
  963. */
  964. @Test
  965. public void basicProxyAuthentication() throws Exception {
  966. final AtomicBoolean finalHostReached = new AtomicBoolean(false);
  967. handler = new RequestHandler() {
  968.  
  969. @Override
  970. public void handle(Request request, HttpServletResponse response) {
  971. finalHostReached.set(true);
  972. response.setStatus(HTTP_OK);
  973. }
  974. };
  975. assertTrue(get(url).useProxy("localhost", proxyPort).proxyBasic("user", "p4ssw0rd").ok());
  976. assertEquals("user", proxyUser.get());
  977. assertEquals("p4ssw0rd", proxyPassword.get());
  978. assertEquals(true, finalHostReached.get());
  979. assertEquals(1, proxyHitCount.get());
  980. }
  981.  
  982. /**
  983. * Make a GET and get response as a input stream reader
  984. *
  985. * @throws Exception
  986. */
  987. @Test
  988. public void getReader() throws Exception {
  989. handler = new RequestHandler() {
  990.  
  991. @Override
  992. public void handle(Request request, HttpServletResponse response) {
  993. response.setStatus(HTTP_OK);
  994. write("hello");
  995. }
  996. };
  997. HttpRequest request = get(url);
  998. assertTrue(request.ok());
  999. BufferedReader reader = new BufferedReader(request.reader());
  1000. assertEquals("hello", reader.readLine());
  1001. reader.close();
  1002. }
  1003.  
  1004. /**
  1005. * Make a POST and send request using a writer
  1006. *
  1007. * @throws Exception
  1008. */
  1009. @Test
  1010. public void sendWithWriter() throws Exception {
  1011. final AtomicReference<String> body = new AtomicReference<String>();
  1012. handler = new RequestHandler() {
  1013.  
  1014. @Override
  1015. public void handle(Request request, HttpServletResponse response) {
  1016. body.set(new String(read()));
  1017. response.setStatus(HTTP_OK);
  1018. }
  1019. };
  1020.  
  1021. HttpRequest request = post(url);
  1022. request.writer().append("hello").close();
  1023. assertTrue(request.ok());
  1024. assertEquals("hello", body.get());
  1025. }
  1026.  
  1027. /**
  1028. * Make a GET and get response as a buffered reader
  1029. *
  1030. * @throws Exception
  1031. */
  1032. @Test
  1033. public void getBufferedReader() throws Exception {
  1034. handler = new RequestHandler() {
  1035.  
  1036. @Override
  1037. public void handle(Request request, HttpServletResponse response) {
  1038. response.setStatus(HTTP_OK);
  1039. write("hello");
  1040. }
  1041. };
  1042. HttpRequest request = get(url);
  1043. assertTrue(request.ok());
  1044. BufferedReader reader = request.bufferedReader();
  1045. assertEquals("hello", reader.readLine());
  1046. reader.close();
  1047. }
  1048.  
  1049. /**
  1050. * Make a GET and get response as a input stream reader
  1051. *
  1052. * @throws Exception
  1053. */
  1054. @Test
  1055. public void getReaderWithCharset() throws Exception {
  1056. handler = new RequestHandler() {
  1057.  
  1058. @Override
  1059. public void handle(Request request, HttpServletResponse response) {
  1060. response.setStatus(HTTP_OK);
  1061. write("hello");
  1062. }
  1063. };
  1064. HttpRequest request = get(url);
  1065. assertTrue(request.ok());
  1066. BufferedReader reader = new BufferedReader(request.reader(CHARSET_UTF8));
  1067. assertEquals("hello", reader.readLine());
  1068. reader.close();
  1069. }
  1070.  
  1071. /**
  1072. * Make a GET and get response body as byte array
  1073. *
  1074. * @throws Exception
  1075. */
  1076. @Test
  1077. public void getBytes() throws Exception {
  1078. handler = new RequestHandler() {
  1079.  
  1080. @Override
  1081. public void handle(Request request, HttpServletResponse response) {
  1082. response.setStatus(HTTP_OK);
  1083. write("hello");
  1084. }
  1085. };
  1086. HttpRequest request = get(url);
  1087. assertTrue(request.ok());
  1088. assertTrue(Arrays.equals("hello".getBytes(), request.bytes()));
  1089. }
  1090.  
  1091. /**
  1092. * Make a GET request that returns an error string
  1093. *
  1094. * @throws Exception
  1095. */
  1096. @Test
  1097. public void getError() throws Exception {
  1098. handler = new RequestHandler() {
  1099.  
  1100. @Override
  1101. public void handle(Request request, HttpServletResponse response) {
  1102. response.setStatus(HttpServletResponse.SC_NOT_FOUND);
  1103. write("error");
  1104. }
  1105. };
  1106. HttpRequest request = get(url);
  1107. assertTrue(request.notFound());
  1108. assertEquals("error", request.body());
  1109. }
  1110.  
  1111. /**
  1112. * Make a GET request that returns an empty error string
  1113. *
  1114. * @throws Exception
  1115. */
  1116. @Test
  1117. public void noError() throws Exception {
  1118. handler = new RequestHandler() {
  1119.  
  1120. @Override
  1121. public void handle(Request request, HttpServletResponse response) {
  1122. response.setStatus(HTTP_OK);
  1123. }
  1124. };
  1125. HttpRequest request = get(url);
  1126. assertTrue(request.ok());
  1127. assertEquals("", request.body());
  1128. }
  1129.  
  1130. /**
  1131. * Verify 'Server' header
  1132. *
  1133. * @throws Exception
  1134. */
  1135. @Test
  1136. public void serverHeader() throws Exception {
  1137. handler = new RequestHandler() {
  1138.  
  1139. @Override
  1140. public void handle(Request request, HttpServletResponse response) {
  1141. response.setStatus(HTTP_OK);
  1142. response.setHeader("Server", "aserver");
  1143. }
  1144. };
  1145. assertEquals("aserver", get(url).server());
  1146. }
  1147.  
  1148. /**
  1149. * Verify 'Expires' header
  1150. *
  1151. * @throws Exception
  1152. */
  1153. @Test
  1154. public void expiresHeader() throws Exception {
  1155. handler = new RequestHandler() {
  1156.  
  1157. @Override
  1158. public void handle(Request request, HttpServletResponse response) {
  1159. response.setStatus(HTTP_OK);
  1160. response.setDateHeader("Expires", 1234000);
  1161. }
  1162. };
  1163. assertEquals(1234000, get(url).expires());
  1164. }
  1165.  
  1166. /**
  1167. * Verify 'Last-Modified' header
  1168. *
  1169. * @throws Exception
  1170. */
  1171. @Test
  1172. public void lastModifiedHeader() throws Exception {
  1173. handler = new RequestHandler() {
  1174.  
  1175. @Override
  1176. public void handle(Request request, HttpServletResponse response) {
  1177. response.setStatus(HTTP_OK);
  1178. response.setDateHeader("Last-Modified", 555000);
  1179. }
  1180. };
  1181. assertEquals(555000, get(url).lastModified());
  1182. }
  1183.  
  1184. /**
  1185. * Verify 'Date' header
  1186. *
  1187. * @throws Exception
  1188. */
  1189. @Test
  1190. public void dateHeader() throws Exception {
  1191. handler = new RequestHandler() {
  1192.  
  1193. @Override
  1194. public void handle(Request request, HttpServletResponse response) {
  1195. response.setStatus(HTTP_OK);
  1196. response.setDateHeader("Date", 66000);
  1197. }
  1198. };
  1199. assertEquals(66000, get(url).date());
  1200. }
  1201.  
  1202. /**
  1203. * Verify 'ETag' header
  1204. *
  1205. * @throws Exception
  1206. */
  1207. @Test
  1208. public void eTagHeader() throws Exception {
  1209. handler = new RequestHandler() {
  1210.  
  1211. @Override
  1212. public void handle(Request request, HttpServletResponse response) {
  1213. response.setStatus(HTTP_OK);
  1214. response.setHeader("ETag", "abcd");
  1215. }
  1216. };
  1217. assertEquals("abcd", get(url).eTag());
  1218. }
  1219.  
  1220. /**
  1221. * Verify 'Location' header
  1222. *
  1223. * @throws Exception
  1224. */
  1225. @Test
  1226. public void locationHeader() throws Exception {
  1227. handler = new RequestHandler() {
  1228.  
  1229. @Override
  1230. public void handle(Request request, HttpServletResponse response) {
  1231. response.setStatus(HTTP_OK);
  1232. response.setHeader("Location", "http://nowhere");
  1233. }
  1234. };
  1235. assertEquals("http://nowhere", get(url).location());
  1236. }
  1237.  
  1238. /**
  1239. * Verify 'Content-Encoding' header
  1240. *
  1241. * @throws Exception
  1242. */
  1243. @Test
  1244. public void contentEncodingHeader() throws Exception {
  1245. handler = new RequestHandler() {
  1246.  
  1247. @Override
  1248. public void handle(Request request, HttpServletResponse response) {
  1249. response.setStatus(HTTP_OK);
  1250. response.setHeader("Content-Encoding", "gzip");
  1251. }
  1252. };
  1253. assertEquals("gzip", get(url).contentEncoding());
  1254. }
  1255.  
  1256. /**
  1257. * Verify 'Content-Type' header
  1258. *
  1259. * @throws Exception
  1260. */
  1261. @Test
  1262. public void contentTypeHeader() throws Exception {
  1263. handler = new RequestHandler() {
  1264.  
  1265. @Override
  1266. public void handle(Request request, HttpServletResponse response) {
  1267. response.setStatus(HTTP_OK);
  1268. response.setHeader("Content-Type", "text/html");
  1269. }
  1270. };
  1271. assertEquals("text/html", get(url).contentType());
  1272. }
  1273.  
  1274. /**
  1275. * Verify 'Content-Type' header
  1276. *
  1277. * @throws Exception
  1278. */
  1279. @Test
  1280. public void requestContentType() throws Exception {
  1281. final AtomicReference<String> contentType = new AtomicReference<String>();
  1282. handler = new RequestHandler() {
  1283.  
  1284. @Override
  1285. public void handle(Request request, HttpServletResponse response) {
  1286. contentType.set(request.getContentType());
  1287. response.setStatus(HTTP_OK);
  1288. }
  1289. };
  1290. assertTrue(post(url).contentType("text/html", "UTF-8").ok());
  1291. assertEquals("text/html; charset=UTF-8", contentType.get());
  1292. }
  1293.  
  1294. /**
  1295. * Verify 'Content-Type' header
  1296. *
  1297. * @throws Exception
  1298. */
  1299. @Test
  1300. public void requestContentTypeNullCharset() throws Exception {
  1301. final AtomicReference<String> contentType = new AtomicReference<String>();
  1302. handler = new RequestHandler() {
  1303.  
  1304. @Override
  1305. public void handle(Request request, HttpServletResponse response) {
  1306. contentType.set(request.getContentType());
  1307. response.setStatus(HTTP_OK);
  1308. }
  1309. };
  1310. assertTrue(post(url).contentType("text/html", null).ok());
  1311. assertEquals("text/html", contentType.get());
  1312. }
  1313.  
  1314. /**
  1315. * Verify 'Content-Type' header
  1316. *
  1317. * @throws Exception
  1318. */
  1319. @Test
  1320. public void requestContentTypeEmptyCharset() throws Exception {
  1321. final AtomicReference<String> contentType = new AtomicReference<String>();
  1322. handler = new RequestHandler() {
  1323.  
  1324. @Override
  1325. public void handle(Request request, HttpServletResponse response) {
  1326. contentType.set(request.getContentType());
  1327. response.setStatus(HTTP_OK);
  1328. }
  1329. };
  1330. assertTrue(post(url).contentType("text/html", "").ok());
  1331. assertEquals("text/html", contentType.get());
  1332. }
  1333.  
  1334. /**
  1335. * Verify 'Cache-Control' header
  1336. *
  1337. * @throws Exception
  1338. */
  1339. @Test
  1340. public void cacheControlHeader() throws Exception {
  1341. handler = new RequestHandler() {
  1342.  
  1343. @Override
  1344. public void handle(Request request, HttpServletResponse response) {
  1345. response.setStatus(HTTP_OK);
  1346. response.setHeader("Cache-Control", "no-cache");
  1347. }
  1348. };
  1349. assertEquals("no-cache", get(url).cacheControl());
  1350. }
  1351.  
  1352. /**
  1353. * Verify setting headers
  1354. *
  1355. * @throws Exception
  1356. */
  1357. @Test
  1358. public void headers() throws Exception {
  1359. final AtomicReference<String> h1 = new AtomicReference<String>();
  1360. final AtomicReference<String> h2 = new AtomicReference<String>();
  1361. handler = new RequestHandler() {
  1362.  
  1363. @Override
  1364. public void handle(Request request, HttpServletResponse response) {
  1365. response.setStatus(HTTP_OK);
  1366. h1.set(request.getHeader("h1"));
  1367. h2.set(request.getHeader("h2"));
  1368. }
  1369. };
  1370. Map<String, String> headers = new HashMap<String, String>();
  1371. headers.put("h1", "v1");
  1372. headers.put("h2", "v2");
  1373. assertTrue(get(url).headers(headers).ok());
  1374. assertEquals("v1", h1.get());
  1375. assertEquals("v2", h2.get());
  1376. }
  1377.  
  1378. /**
  1379. * Verify setting headers
  1380. *
  1381. * @throws Exception
  1382. */
  1383. @Test
  1384. public void emptyHeaders() throws Exception {
  1385. handler = new RequestHandler() {
  1386.  
  1387. @Override
  1388. public void handle(Request request, HttpServletResponse response) {
  1389. response.setStatus(HTTP_OK);
  1390. }
  1391. };
  1392. assertTrue(get(url).headers(Collections.<String, String> emptyMap()).ok());
  1393. }
  1394.  
  1395. /**
  1396. * Verify getting all headers
  1397. *
  1398. * @throws Exception
  1399. */
  1400. @Test
  1401. public void getAllHeaders() throws Exception {
  1402. handler = new RequestHandler() {
  1403.  
  1404. @Override
  1405. public void handle(Request request, HttpServletResponse response) {
  1406. response.setStatus(HTTP_OK);
  1407. response.setHeader("a", "a");
  1408. response.setHeader("b", "b");
  1409. response.addHeader("a", "another");
  1410. }
  1411. };
  1412. Map<String, List<String>> headers = get(url).headers();
  1413. assertEquals(headers.size(), 5);
  1414. assertEquals(headers.get("a").size(), 2);
  1415. assertTrue(headers.get("b").get(0).equals("b"));
  1416. }
  1417.  
  1418. /**
  1419. * Verify setting number header
  1420. *
  1421. * @throws Exception
  1422. */
  1423. @Test
  1424. public void numberHeader() throws Exception {
  1425. final AtomicReference<String> h1 = new AtomicReference<String>();
  1426. final AtomicReference<String> h2 = new AtomicReference<String>();
  1427. handler = new RequestHandler() {
  1428.  
  1429. @Override
  1430. public void handle(Request request, HttpServletResponse response) {
  1431. response.setStatus(HTTP_OK);
  1432. h1.set(request.getHeader("h1"));
  1433. h2.set(request.getHeader("h2"));
  1434. }
  1435. };
  1436. assertTrue(get(url).header("h1", 5).header("h2", (Number) null).ok());
  1437. assertEquals("5", h1.get());
  1438. assertEquals("", h2.get());
  1439. }
  1440.  
  1441. /**
  1442. * Verify 'User-Agent' request header
  1443. *
  1444. * @throws Exception
  1445. */
  1446. @Test
  1447. public void userAgentHeader() throws Exception {
  1448. final AtomicReference<String> header = new AtomicReference<String>();
  1449. handler = new RequestHandler() {
  1450.  
  1451. @Override
  1452. public void handle(Request request, HttpServletResponse response) {
  1453. response.setStatus(HTTP_OK);
  1454. header.set(request.getHeader("User-Agent"));
  1455. }
  1456. };
  1457. assertTrue(get(url).userAgent("browser 1.0").ok());
  1458. assertEquals("browser 1.0", header.get());
  1459. }
  1460.  
  1461. /**
  1462. * Verify 'Accept' request header
  1463. *
  1464. * @throws Exception
  1465. */
  1466. @Test
  1467. public void acceptHeader() throws Exception {
  1468. final AtomicReference<String> header = new AtomicReference<String>();
  1469. handler = new RequestHandler() {
  1470.  
  1471. @Override
  1472. public void handle(Request request, HttpServletResponse response) {
  1473. response.setStatus(HTTP_OK);
  1474. header.set(request.getHeader("Accept"));
  1475. }
  1476. };
  1477. assertTrue(get(url).accept("application/json").ok());
  1478. assertEquals("application/json", header.get());
  1479. }
  1480.  
  1481. /**
  1482. * Verify 'Accept' request header when calling
  1483. * {@link HttpRequest#acceptJson()}
  1484. *
  1485. * @throws Exception
  1486. */
  1487. @Test
  1488. public void acceptJson() throws Exception {
  1489. final AtomicReference<String> header = new AtomicReference<String>();
  1490. handler = new RequestHandler() {
  1491.  
  1492. @Override
  1493. public void handle(Request request, HttpServletResponse response) {
  1494. response.setStatus(HTTP_OK);
  1495. header.set(request.getHeader("Accept"));
  1496. }
  1497. };
  1498. assertTrue(get(url).acceptJson().ok());
  1499. assertEquals("application/json", header.get());
  1500. }
  1501.  
  1502. /**
  1503. * Verify 'If-None-Match' request header
  1504. *
  1505. * @throws Exception
  1506. */
  1507. @Test
  1508. public void ifNoneMatchHeader() throws Exception {
  1509. final AtomicReference<String> header = new AtomicReference<String>();
  1510. handler = new RequestHandler() {
  1511.  
  1512. @Override
  1513. public void handle(Request request, HttpServletResponse response) {
  1514. response.setStatus(HTTP_OK);
  1515. header.set(request.getHeader("If-None-Match"));
  1516. }
  1517. };
  1518. assertTrue(get(url).ifNoneMatch("eid").ok());
  1519. assertEquals("eid", header.get());
  1520. }
  1521.  
  1522. /**
  1523. * Verify 'Accept-Charset' request header
  1524. *
  1525. * @throws Exception
  1526. */
  1527. @Test
  1528. public void acceptCharsetHeader() throws Exception {
  1529. final AtomicReference<String> header = new AtomicReference<String>();
  1530. handler = new RequestHandler() {
  1531.  
  1532. @Override
  1533. public void handle(Request request, HttpServletResponse response) {
  1534. response.setStatus(HTTP_OK);
  1535. header.set(request.getHeader("Accept-Charset"));
  1536. }
  1537. };
  1538. assertTrue(get(url).acceptCharset(CHARSET_UTF8).ok());
  1539. assertEquals(CHARSET_UTF8, header.get());
  1540. }
  1541.  
  1542. /**
  1543. * Verify 'Accept-Encoding' request header
  1544. *
  1545. * @throws Exception
  1546. */
  1547. @Test
  1548. public void acceptEncodingHeader() throws Exception {
  1549. final AtomicReference<String> header = new AtomicReference<String>();
  1550. handler = new RequestHandler() {
  1551.  
  1552. @Override
  1553. public void handle(Request request, HttpServletResponse response) {
  1554. response.setStatus(HTTP_OK);
  1555. header.set(request.getHeader("Accept-Encoding"));
  1556. }
  1557. };
  1558. assertTrue(get(url).acceptEncoding("compress").ok());
  1559. assertEquals("compress", header.get());
  1560. }
  1561.  
  1562. /**
  1563. * Verify 'If-Modified-Since' request header
  1564. *
  1565. * @throws Exception
  1566. */
  1567. @Test
  1568. public void ifModifiedSinceHeader() throws Exception {
  1569. final AtomicLong header = new AtomicLong();
  1570. handler = new RequestHandler() {
  1571.  
  1572. @Override
  1573. public void handle(Request request, HttpServletResponse response) {
  1574. response.setStatus(HTTP_OK);
  1575. header.set(request.getDateHeader("If-Modified-Since"));
  1576. }
  1577. };
  1578. assertTrue(get(url).ifModifiedSince(5000).ok());
  1579. assertEquals(5000, header.get());
  1580. }
  1581.  
  1582. /**
  1583. * Verify 'Referer' header
  1584. *
  1585. * @throws Exception
  1586. */
  1587. @Test
  1588. public void refererHeader() throws Exception {
  1589. final AtomicReference<String> referer = new AtomicReference<String>();
  1590. handler = new RequestHandler() {
  1591.  
  1592. @Override
  1593. public void handle(Request request, HttpServletResponse response) {
  1594. referer.set(request.getHeader("Referer"));
  1595. response.setStatus(HTTP_OK);
  1596. }
  1597. };
  1598. assertTrue(post(url).referer("http://heroku.com").ok());
  1599. assertEquals("http://heroku.com", referer.get());
  1600. }
  1601.  
  1602. /**
  1603. * Verify multipart with file, stream, number, and string parameters
  1604. *
  1605. * @throws Exception
  1606. */
  1607. @Test
  1608. public void postMultipart() throws Exception {
  1609. final StringBuilder body = new StringBuilder();
  1610. handler = new RequestHandler() {
  1611.  
  1612. @Override
  1613. public void handle(Request request, HttpServletResponse response) {
  1614. response.setStatus(HTTP_OK);
  1615. char[] buffer = new char[8192];
  1616. int read;
  1617. try {
  1618. while ((read = request.getReader().read(buffer)) != -1)
  1619. body.append(buffer, 0, read);
  1620. } catch (IOException e) {
  1621. fail();
  1622. }
  1623. }
  1624. };
  1625. File file = File.createTempFile("body", ".txt");
  1626. File file2 = File.createTempFile("body", ".txt");
  1627. new FileWriter(file).append("content1").close();
  1628. new FileWriter(file2).append("content4").close();
  1629. HttpRequest request = post(url);
  1630. request.part("description", "content2");
  1631. request.part("size", file.length());
  1632. request.part("body", file.getName(), file);
  1633. request.part("file", file2);
  1634. request.part("stream", new ByteArrayInputStream("content3".getBytes()));
  1635. assertTrue(request.ok());
  1636. assertTrue(body.toString().contains("content1\r\n"));
  1637. assertTrue(body.toString().contains("content2\r\n"));
  1638. assertTrue(body.toString().contains("content3\r\n"));
  1639. assertTrue(body.toString().contains("content4\r\n"));
  1640. assertTrue(body.toString().contains(Long.toString(file.length()) + "\r\n"));
  1641. }
  1642.  
  1643. /**
  1644. * Verify multipart with content type part header
  1645. *
  1646. * @throws Exception
  1647. */
  1648. @Test
  1649. public void postMultipartWithContentType() throws Exception {
  1650. final AtomicReference<String> body = new AtomicReference<String>();
  1651. handler = new RequestHandler() {
  1652.  
  1653. @Override
  1654. public void handle(Request request, HttpServletResponse response) {
  1655. response.setStatus(HTTP_OK);
  1656. body.set(new String(read()));
  1657. }
  1658. };
  1659. HttpRequest request = post(url);
  1660. request.part("body", null, "application/json", "contents");
  1661. assertTrue(request.ok());
  1662. assertTrue(body.toString().contains("Content-Type: application/json"));
  1663. assertTrue(body.toString().contains("contents\r\n"));
  1664. }
  1665.  
  1666. /**
  1667. * Verify response in {@link Appendable}
  1668. *
  1669. * @throws Exception
  1670. */
  1671. @Test
  1672. public void receiveAppendable() throws Exception {
  1673. final StringBuilder body = new StringBuilder();
  1674. handler = new RequestHandler() {
  1675.  
  1676. @Override
  1677. public void handle(Request request, HttpServletResponse response) {
  1678. response.setStatus(HTTP_OK);
  1679. try {
  1680. response.getWriter().print("content");
  1681. } catch (IOException e) {
  1682. fail();
  1683. }
  1684. }
  1685. };
  1686. assertTrue(post(url).receive(body).ok());
  1687. assertEquals("content", body.toString());
  1688. }
  1689.  
  1690. /**
  1691. * Verify response in {@link Writer}
  1692. *
  1693. * @throws Exception
  1694. */
  1695. @Test
  1696. public void receiveWriter() throws Exception {
  1697. handler = new RequestHandler() {
  1698.  
  1699. @Override
  1700. public void handle(Request request, HttpServletResponse response) {
  1701. response.setStatus(HTTP_OK);
  1702. try {
  1703. response.getWriter().print("content");
  1704. } catch (IOException e) {
  1705. fail();
  1706. }
  1707. }
  1708. };
  1709. StringWriter writer = new StringWriter();
  1710. assertTrue(post(url).receive(writer).ok());
  1711. assertEquals("content", writer.toString());
  1712. }
  1713.  
  1714. /**
  1715. * Verify response via a {@link PrintStream}
  1716. *
  1717. * @throws Exception
  1718. */
  1719. @Test
  1720. public void receivePrintStream() throws Exception {
  1721. handler = new RequestHandler() {
  1722.  
  1723. @Override
  1724. public void handle(Request request, HttpServletResponse response) {
  1725. response.setStatus(HTTP_OK);
  1726. try {
  1727. response.getWriter().print("content");
  1728. } catch (IOException e) {
  1729. fail();
  1730. }
  1731. }
  1732. };
  1733. ByteArrayOutputStream output = new ByteArrayOutputStream();
  1734. PrintStream stream = new PrintStream(output, true, CHARSET_UTF8);
  1735. assertTrue(post(url).receive(stream).ok());
  1736. stream.close();
  1737. assertEquals("content", output.toString(CHARSET_UTF8));
  1738. }
  1739.  
  1740. /**
  1741. * Verify response in {@link File}
  1742. *
  1743. * @throws Exception
  1744. */
  1745. @Test
  1746. public void receiveFile() throws Exception {
  1747. handler = new RequestHandler() {
  1748.  
  1749. @Override
  1750. public void handle(Request request, HttpServletResponse response) {
  1751. response.setStatus(HTTP_OK);
  1752. try {
  1753. response.getWriter().print("content");
  1754. } catch (IOException e) {
  1755. fail();
  1756. }
  1757. }
  1758. };
  1759. File output = File.createTempFile("output", ".txt");
  1760. assertTrue(post(url).receive(output).ok());
  1761. StringBuilder buffer = new StringBuilder();
  1762. BufferedReader reader = new BufferedReader(new FileReader(output));
  1763. int read;
  1764. while ((read = reader.read()) != -1)
  1765. buffer.append((char) read);
  1766. reader.close();
  1767. assertEquals("content", buffer.toString());
  1768. }
  1769.  
  1770. /**
  1771. * Verify certificate and host helpers on HTTPS connection
  1772. *
  1773. * @throws Exception
  1774. */
  1775. @Test
  1776. public void httpsTrust() throws Exception {
  1777. assertNotNull(get("https://localhost").trustAllCerts().trustAllHosts());
  1778. }
  1779.  
  1780. /**
  1781. * Verify certificate and host helpers ignore non-HTTPS connection
  1782. *
  1783. * @throws Exception
  1784. */
  1785. @Test
  1786. public void httpTrust() throws Exception {
  1787. assertNotNull(get("http://localhost").trustAllCerts().trustAllHosts());
  1788. }
  1789.  
  1790. /**
  1791. * Verify hostname verifier is set and accepts all
  1792. */
  1793. @Test
  1794. public void verifierAccepts() {
  1795. HttpRequest request = get("https://localhost");
  1796. HttpsURLConnection connection = (HttpsURLConnection) request
  1797. .getConnection();
  1798. request.trustAllHosts();
  1799. assertNotNull(connection.getHostnameVerifier());
  1800. assertTrue(connection.getHostnameVerifier().verify(null, null));
  1801. }
  1802.  
  1803. /**
  1804. * Verify single hostname verifier is created across all calls
  1805. */
  1806. @Test
  1807. public void singleVerifier() {
  1808. HttpRequest request1 = get("https://localhost").trustAllHosts();
  1809. HttpRequest request2 = get("https://localhost").trustAllHosts();
  1810. assertNotNull(((HttpsURLConnection) request1.getConnection())
  1811. .getHostnameVerifier());
  1812. assertNotNull(((HttpsURLConnection) request2.getConnection())
  1813. .getHostnameVerifier());
  1814. assertEquals(
  1815. ((HttpsURLConnection) request1.getConnection()).getHostnameVerifier(),
  1816. ((HttpsURLConnection) request2.getConnection()).getHostnameVerifier());
  1817. }
  1818.  
  1819. /**
  1820. * Verify single SSL socket factory is created across all calls
  1821. */
  1822. @Test
  1823. public void singleSslSocketFactory() {
  1824. HttpRequest request1 = get("https://localhost").trustAllCerts();
  1825. HttpRequest request2 = get("https://localhost").trustAllCerts();
  1826. assertNotNull(((HttpsURLConnection) request1.getConnection())
  1827. .getSSLSocketFactory());
  1828. assertNotNull(((HttpsURLConnection) request2.getConnection())
  1829. .getSSLSocketFactory());
  1830. assertEquals(
  1831. ((HttpsURLConnection) request1.getConnection()).getSSLSocketFactory(),
  1832. ((HttpsURLConnection) request2.getConnection()).getSSLSocketFactory());
  1833. }
  1834.  
  1835. /**
  1836. * Send a stream that throws an exception when read from
  1837. *
  1838. * @throws Exception
  1839. */
  1840. @Test
  1841. public void sendErrorReadStream() throws Exception {
  1842. handler = new RequestHandler() {
  1843.  
  1844. @Override
  1845. public void handle(Request request, HttpServletResponse response) {
  1846. response.setStatus(HTTP_OK);
  1847. try {
  1848. response.getWriter().print("content");
  1849. } catch (IOException e) {
  1850. fail();
  1851. }
  1852. }
  1853. };
  1854. final IOException readCause = new IOException();
  1855. final IOException closeCause = new IOException();
  1856. InputStream stream = new InputStream() {
  1857.  
  1858. public int read() throws IOException {
  1859. throw readCause;
  1860. }
  1861.  
  1862. public void close() throws IOException {
  1863. throw closeCause;
  1864. }
  1865. };
  1866. try {
  1867. post(url).send(stream);
  1868. fail("Exception not thrown");
  1869. } catch (HttpRequestException e) {
  1870. assertEquals(readCause, e.getCause());
  1871. }
  1872. }
  1873.  
  1874. /**
  1875. * Send a stream that throws an exception when read from
  1876. *
  1877. * @throws Exception
  1878. */
  1879. @Test
  1880. public void sendErrorCloseStream() throws Exception {
  1881. handler = new RequestHandler() {
  1882.  
  1883. @Override
  1884. public void handle(Request request, HttpServletResponse response) {
  1885. response.setStatus(HTTP_OK);
  1886. try {
  1887. response.getWriter().print("content");
  1888. } catch (IOException e) {
  1889. fail();
  1890. }
  1891. }
  1892. };
  1893. final IOException closeCause = new IOException();
  1894. InputStream stream = new InputStream() {
  1895.  
  1896. public int read() throws IOException {
  1897. return -1;
  1898. }
  1899.  
  1900. public void close() throws IOException {
  1901. throw closeCause;
  1902. }
  1903. };
  1904. try {
  1905. post(url).ignoreCloseExceptions(false).send(stream);
  1906. fail("Exception not thrown");
  1907. } catch (HttpRequestException e) {
  1908. assertEquals(closeCause, e.getCause());
  1909. }
  1910. }
  1911.  
  1912. /**
  1913. * Make a GET request and get the code using an {@link AtomicInteger}
  1914. *
  1915. * @throws Exception
  1916. */
  1917. @Test
  1918. public void getToOutputCode() throws Exception {
  1919. handler = new RequestHandler() {
  1920.  
  1921. @Override
  1922. public void handle(Request request, HttpServletResponse response) {
  1923. response.setStatus(HTTP_OK);
  1924. }
  1925. };
  1926. AtomicInteger code = new AtomicInteger(0);
  1927. get(url).code(code);
  1928. assertEquals(HTTP_OK, code.get());
  1929. }
  1930.  
  1931. /**
  1932. * Make a GET request and get the body using an {@link AtomicReference}
  1933. *
  1934. * @throws Exception
  1935. */
  1936. @Test
  1937. public void getToOutputBody() throws Exception {
  1938. handler = new RequestHandler() {
  1939.  
  1940. @Override
  1941. public void handle(Request request, HttpServletResponse response) {
  1942. response.setStatus(HTTP_OK);
  1943. try {
  1944. response.getWriter().print("hello world");
  1945. } catch (IOException e) {
  1946. fail();
  1947. }
  1948. }
  1949. };
  1950. AtomicReference<String> body = new AtomicReference<String>(null);
  1951. get(url).body(body);
  1952. assertEquals("hello world", body.get());
  1953. }
  1954.  
  1955. /**
  1956. * Make a GET request and get the body using an {@link AtomicReference}
  1957. *
  1958. * @throws Exception
  1959. */
  1960. @Test
  1961. public void getToOutputBodyWithCharset() throws Exception {
  1962. handler = new RequestHandler() {
  1963.  
  1964. @Override
  1965. public void handle(Request request, HttpServletResponse response) {
  1966. response.setStatus(HTTP_OK);
  1967. try {
  1968. response.getWriter().print("hello world");
  1969. } catch (IOException e) {
  1970. fail();
  1971. }
  1972. }
  1973. };
  1974. AtomicReference<String> body = new AtomicReference<String>(null);
  1975. get(url).body(body, CHARSET_UTF8);
  1976. assertEquals("hello world", body.get());
  1977. }
  1978.  
  1979.  
  1980. /**
  1981. * Make a GET request that should be compressed
  1982. *
  1983. * @throws Exception
  1984. */
  1985. @Test
  1986. public void getGzipped() throws Exception {
  1987. handler = new RequestHandler() {
  1988.  
  1989. @Override
  1990. public void handle(Request request, HttpServletResponse response) {
  1991. response.setStatus(HTTP_OK);
  1992. if (!"gzip".equals(request.getHeader("Accept-Encoding")))
  1993. return;
  1994.  
  1995. response.setHeader("Content-Encoding", "gzip");
  1996. GZIPOutputStream output;
  1997. try {
  1998. output = new GZIPOutputStream(response.getOutputStream());
  1999. } catch (IOException e) {
  2000. throw new RuntimeException(e);
  2001. }
  2002. try {
  2003. output.write("hello compressed".getBytes(CHARSET_UTF8));
  2004. } catch (IOException e) {
  2005. throw new RuntimeException(e);
  2006. } finally {
  2007. try {
  2008. output.close();
  2009. } catch (IOException ignored) {
  2010. // Ignored
  2011. }
  2012. }
  2013. }
  2014. };
  2015. HttpRequest request = get(url).acceptGzipEncoding().uncompress(true);
  2016. assertTrue(request.ok());
  2017. assertEquals("hello compressed", request.body(CHARSET_UTF8));
  2018. }
  2019.  
  2020. /**
  2021. * Make a GET request that should be compressed but isn't
  2022. *
  2023. * @throws Exception
  2024. */
  2025. @Test
  2026. public void getNonGzippedWithUncompressEnabled() throws Exception {
  2027. handler = new RequestHandler() {
  2028.  
  2029. @Override
  2030. public void handle(Request request, HttpServletResponse response) {
  2031. response.setStatus(HTTP_OK);
  2032. if (!"gzip".equals(request.getHeader("Accept-Encoding")))
  2033. return;
  2034.  
  2035. write("hello not compressed");
  2036. }
  2037. };
  2038. HttpRequest request = get(url).acceptGzipEncoding().uncompress(true);
  2039. assertTrue(request.ok());
  2040. assertEquals("hello not compressed", request.body(CHARSET_UTF8));
  2041. }
  2042.  
  2043. /**
  2044. * Get header with multiple response values
  2045. *
  2046. * @throws Exception
  2047. */
  2048. @Test
  2049. public void getHeaders() throws Exception {
  2050. handler = new RequestHandler() {
  2051.  
  2052. @Override
  2053. public void handle(Request request, HttpServletResponse response) {
  2054. response.setStatus(HTTP_OK);
  2055. response.addHeader("a", "1");
  2056. response.addHeader("a", "2");
  2057. }
  2058. };
  2059. HttpRequest request = get(url);
  2060. assertTrue(request.ok());
  2061. String[] values = request.headers("a");
  2062. assertNotNull(values);
  2063. assertEquals(2, values.length);
  2064. assertTrue(Arrays.asList(values).contains("1"));
  2065. assertTrue(Arrays.asList(values).contains("2"));
  2066. }
  2067.  
  2068. /**
  2069. * Get header values when not set in response
  2070. *
  2071. * @throws Exception
  2072. */
  2073. @Test
  2074. public void getEmptyHeaders() throws Exception {
  2075. handler = new RequestHandler() {
  2076.  
  2077. @Override
  2078. public void handle(Request request, HttpServletResponse response) {
  2079. response.setStatus(HTTP_OK);
  2080. }
  2081. };
  2082. HttpRequest request = get(url);
  2083. assertTrue(request.ok());
  2084. String[] values = request.headers("a");
  2085. assertNotNull(values);
  2086. assertEquals(0, values.length);
  2087. }
  2088.  
  2089. /**
  2090. * Get header parameter value
  2091. *
  2092. * @throws Exception
  2093. */
  2094. @Test
  2095. public void getSingleParameter() throws Exception {
  2096. handler = new RequestHandler() {
  2097.  
  2098. @Override
  2099. public void handle(Request request, HttpServletResponse response) {
  2100. response.setStatus(HTTP_OK);
  2101. response.setHeader("a", "b;c=d");
  2102. }
  2103. };
  2104. HttpRequest request = get(url);
  2105. assertTrue(request.ok());
  2106. assertEquals("d", request.parameter("a", "c"));
  2107. }
  2108.  
  2109. /**
  2110. * Get header parameter value
  2111. *
  2112. * @throws Exception
  2113. */
  2114. @Test
  2115. public void getMultipleParameters() throws Exception {
  2116. handler = new RequestHandler() {
  2117.  
  2118. @Override
  2119. public void handle(Request request, HttpServletResponse response) {
  2120. response.setStatus(HTTP_OK);
  2121. response.setHeader("a", "b;c=d;e=f");
  2122. }
  2123. };
  2124. HttpRequest request = get(url);
  2125. assertTrue(request.ok());
  2126. assertEquals("d", request.parameter("a", "c"));
  2127. assertEquals("f", request.parameter("a", "e"));
  2128. }
  2129.  
  2130. /**
  2131. * Get header parameter value
  2132. *
  2133. * @throws Exception
  2134. */
  2135. @Test
  2136. public void getSingleParameterQuoted() throws Exception {
  2137. handler = new RequestHandler() {
  2138.  
  2139. @Override
  2140. public void handle(Request request, HttpServletResponse response) {
  2141. response.setStatus(HTTP_OK);
  2142. response.setHeader("a", "b;c=\"d\"");
  2143. }
  2144. };
  2145. HttpRequest request = get(url);
  2146. assertTrue(request.ok());
  2147. assertEquals("d", request.parameter("a", "c"));
  2148. }
  2149.  
  2150. /**
  2151. * Get header parameter value
  2152. *
  2153. * @throws Exception
  2154. */
  2155. @Test
  2156. public void getMultipleParametersQuoted() throws Exception {
  2157. handler = new RequestHandler() {
  2158.  
  2159. @Override
  2160. public void handle(Request request, HttpServletResponse response) {
  2161. response.setStatus(HTTP_OK);
  2162. response.setHeader("a", "b;c=\"d\";e=\"f\"");
  2163. }
  2164. };
  2165. HttpRequest request = get(url);
  2166. assertTrue(request.ok());
  2167. assertEquals("d", request.parameter("a", "c"));
  2168. assertEquals("f", request.parameter("a", "e"));
  2169. }
  2170.  
  2171. /**
  2172. * Get header parameter value
  2173. *
  2174. * @throws Exception
  2175. */
  2176. @Test
  2177. public void getMissingParameter() throws Exception {
  2178. handler = new RequestHandler() {
  2179.  
  2180. @Override
  2181. public void handle(Request request, HttpServletResponse response) {
  2182. response.setStatus(HTTP_OK);
  2183. response.setHeader("a", "b;c=d");
  2184. }
  2185. };
  2186. HttpRequest request = get(url);
  2187. assertTrue(request.ok());
  2188. assertNull(request.parameter("a", "e"));
  2189. }
  2190.  
  2191. /**
  2192. * Get header parameter value
  2193. *
  2194. * @throws Exception
  2195. */
  2196. @Test
  2197. public void getParameterFromMissingHeader() throws Exception {
  2198. handler = new RequestHandler() {
  2199.  
  2200. @Override
  2201. public void handle(Request request, HttpServletResponse response) {
  2202. response.setStatus(HTTP_OK);
  2203. response.setHeader("a", "b;c=d");
  2204. }
  2205. };
  2206. HttpRequest request = get(url);
  2207. assertTrue(request.ok());
  2208. assertNull(request.parameter("b", "c"));
  2209. assertTrue(request.parameters("b").isEmpty());
  2210. }
  2211.  
  2212. /**
  2213. * Get header parameter value
  2214. *
  2215. * @throws Exception
  2216. */
  2217. @Test
  2218. public void getEmptyParameter() throws Exception {
  2219. handler = new RequestHandler() {
  2220.  
  2221. @Override
  2222. public void handle(Request request, HttpServletResponse response) {
  2223. response.setStatus(HTTP_OK);
  2224. response.setHeader("a", "b;c=");
  2225. }
  2226. };
  2227. HttpRequest request = get(url);
  2228. assertTrue(request.ok());
  2229. assertNull(request.parameter("a", "c"));
  2230. assertTrue(request.parameters("a").isEmpty());
  2231. }
  2232.  
  2233. /**
  2234. * Get header parameter value
  2235. *
  2236. * @throws Exception
  2237. */
  2238. @Test
  2239. public void getEmptyParameters() throws Exception {
  2240. handler = new RequestHandler() {
  2241.  
  2242. @Override
  2243. public void handle(Request request, HttpServletResponse response) {
  2244. response.setStatus(HTTP_OK);
  2245. response.setHeader("a", "b;");
  2246. }
  2247. };
  2248. HttpRequest request = get(url);
  2249. assertTrue(request.ok());
  2250. assertNull(request.parameter("a", "c"));
  2251. assertTrue(request.parameters("a").isEmpty());
  2252. }
  2253.  
  2254. /**
  2255. * Get header parameter values
  2256. *
  2257. * @throws Exception
  2258. */
  2259. @Test
  2260. public void getParameters() throws Exception {
  2261. handler = new RequestHandler() {
  2262.  
  2263. @Override
  2264. public void handle(Request request, HttpServletResponse response) {
  2265. response.setStatus(HTTP_OK);
  2266. response.setHeader("a", "value;b=c;d=e");
  2267. }
  2268. };
  2269. HttpRequest request = get(url);
  2270. assertTrue(request.ok());
  2271. Map<String, String> params = request.parameters("a");
  2272. assertNotNull(params);
  2273. assertEquals(2, params.size());
  2274. assertEquals("c", params.get("b"));
  2275. assertEquals("e", params.get("d"));
  2276. }
  2277.  
  2278. /**
  2279. * Get header parameter values
  2280. *
  2281. * @throws Exception
  2282. */
  2283. @Test
  2284. public void getQuotedParameters() throws Exception {
  2285. handler = new RequestHandler() {
  2286.  
  2287. @Override
  2288. public void handle(Request request, HttpServletResponse response) {
  2289. response.setStatus(HTTP_OK);
  2290. response.setHeader("a", "value;b=\"c\";d=\"e\"");
  2291. }
  2292. };
  2293. HttpRequest request = get(url);
  2294. assertTrue(request.ok());
  2295. Map<String, String> params = request.parameters("a");
  2296. assertNotNull(params);
  2297. assertEquals(2, params.size());
  2298. assertEquals("c", params.get("b"));
  2299. assertEquals("e", params.get("d"));
  2300. }
  2301.  
  2302. /**
  2303. * Get header parameter values
  2304. *
  2305. * @throws Exception
  2306. */
  2307. @Test
  2308. public void getMixQuotedParameters() throws Exception {
  2309. handler = new RequestHandler() {
  2310.  
  2311. @Override
  2312. public void handle(Request request, HttpServletResponse response) {
  2313. response.setStatus(HTTP_OK);
  2314. response.setHeader("a", "value; b=c; d=\"e\"");
  2315. }
  2316. };
  2317. HttpRequest request = get(url);
  2318. assertTrue(request.ok());
  2319. Map<String, String> params = request.parameters("a");
  2320. assertNotNull(params);
  2321. assertEquals(2, params.size());
  2322. assertEquals("c", params.get("b"));
  2323. assertEquals("e", params.get("d"));
  2324. }
  2325.  
  2326. /**
  2327. * Verify getting date header with default value
  2328. *
  2329. * @throws Exception
  2330. */
  2331. @Test
  2332. public void missingDateHeader() throws Exception {
  2333. handler = new RequestHandler() {
  2334.  
  2335. @Override
  2336. public void handle(Request request, HttpServletResponse response) {
  2337. response.setStatus(HTTP_OK);
  2338. }
  2339. };
  2340. assertEquals(1234L, get(url).dateHeader("missing", 1234L));
  2341. }
  2342.  
  2343. /**
  2344. * Verify getting date header with default value
  2345. *
  2346. * @throws Exception
  2347. */
  2348. @Test
  2349. public void malformedDateHeader() throws Exception {
  2350. handler = new RequestHandler() {
  2351.  
  2352. @Override
  2353. public void handle(Request request, HttpServletResponse response) {
  2354. response.setStatus(HTTP_OK);
  2355. response.setHeader("malformed", "not a date");
  2356. }
  2357. };
  2358. assertEquals(1234L, get(url).dateHeader("malformed", 1234L));
  2359. }
  2360.  
  2361. /**
  2362. * Verify getting int header with default value
  2363. *
  2364. * @throws Exception
  2365. */
  2366. @Test
  2367. public void missingIntHeader() throws Exception {
  2368. handler = new RequestHandler() {
  2369.  
  2370. @Override
  2371. public void handle(Request request, HttpServletResponse response) {
  2372. response.setStatus(HTTP_OK);
  2373. }
  2374. };
  2375. assertEquals(4321, get(url).intHeader("missing", 4321));
  2376. }
  2377.  
  2378. /**
  2379. * Verify getting int header with default value
  2380. *
  2381. * @throws Exception
  2382. */
  2383. @Test
  2384. public void malformedIntHeader() throws Exception {
  2385. handler = new RequestHandler() {
  2386.  
  2387. @Override
  2388. public void handle(Request request, HttpServletResponse response) {
  2389. response.setStatus(HTTP_OK);
  2390. response.setHeader("malformed", "not an integer");
  2391. }
  2392. };
  2393. assertEquals(4321, get(url).intHeader("malformed", 4321));
  2394. }
  2395.  
  2396. /**
  2397. * Verify sending form data as a sequence of {@link Entry} objects
  2398. *
  2399. * @throws Exception
  2400. */
  2401. @Test
  2402. public void postFormAsEntries() throws Exception {
  2403. final AtomicReference<String> body = new AtomicReference<String>();
  2404. handler = new RequestHandler() {
  2405.  
  2406. @Override
  2407. public void handle(Request request, HttpServletResponse response) {
  2408. body.set(new String(read()));
  2409. response.setStatus(HTTP_OK);
  2410. }
  2411. };
  2412. Map<String, String> data = new LinkedHashMap<String, String>();
  2413. data.put("name", "user");
  2414. data.put("number", "100");
  2415. HttpRequest request = post(url);
  2416. for (Entry<String, String> entry : data.entrySet())
  2417. request.form(entry);
  2418. int code = request.code();
  2419. assertEquals(HTTP_OK, code);
  2420. assertEquals("name=user&number=100", body.get());
  2421. }
  2422.  
  2423. /**
  2424. * Verify sending form data where entry value is null
  2425. *
  2426. * @throws Exception
  2427. */
  2428. @Test
  2429. public void postFormEntryWithNullValue() throws Exception {
  2430. final AtomicReference<String> body = new AtomicReference<String>();
  2431. handler = new RequestHandler() {
  2432.  
  2433. @Override
  2434. public void handle(Request request, HttpServletResponse response) {
  2435. body.set(new String(read()));
  2436. response.setStatus(HTTP_OK);
  2437. }
  2438. };
  2439. Map<String, String> data = new LinkedHashMap<String, String>();
  2440. data.put("name", null);
  2441. HttpRequest request = post(url);
  2442. for (Entry<String, String> entry : data.entrySet())
  2443. request.form(entry);
  2444. int code = request.code();
  2445. assertEquals(HTTP_OK, code);
  2446. assertEquals("name=", body.get());
  2447. }
  2448.  
  2449. /**
  2450. * Verify POST with query parameters
  2451. *
  2452. * @throws Exception
  2453. */
  2454. @Test
  2455. public void postWithMappedQueryParams() throws Exception {
  2456. Map<String, String> inputParams = new HashMap<String, String>();
  2457. inputParams.put("name", "user");
  2458. inputParams.put("number", "100");
  2459. final Map<String, String> outputParams = new HashMap<String, String>();
  2460. final AtomicReference<String> method = new AtomicReference<String>();
  2461. handler = new RequestHandler() {
  2462.  
  2463. @Override
  2464. public void handle(Request request, HttpServletResponse response) {
  2465. method.set(request.getMethod());
  2466. outputParams.put("name", request.getParameter("name"));
  2467. outputParams.put("number", request.getParameter("number"));
  2468. response.setStatus(HTTP_OK);
  2469. }
  2470. };
  2471. HttpRequest request = post(url, inputParams, false);
  2472. assertTrue(request.ok());
  2473. assertEquals("POST", method.get());
  2474. assertEquals("user", outputParams.get("name"));
  2475. assertEquals("100", outputParams.get("number"));
  2476. }
  2477.  
  2478. /**
  2479. * Verify POST with query parameters
  2480. *
  2481. * @throws Exception
  2482. */
  2483. @Test
  2484. public void postWithVaragsQueryParams() throws Exception {
  2485. final Map<String, String> outputParams = new HashMap<String, String>();
  2486. final AtomicReference<String> method = new AtomicReference<String>();
  2487. handler = new RequestHandler() {
  2488.  
  2489. @Override
  2490. public void handle(Request request, HttpServletResponse response) {
  2491. method.set(request.getMethod());
  2492. outputParams.put("name", request.getParameter("name"));
  2493. outputParams.put("number", request.getParameter("number"));
  2494. response.setStatus(HTTP_OK);
  2495. }
  2496. };
  2497. HttpRequest request = post(url, false, "name", "user", "number", "100");
  2498. assertTrue(request.ok());
  2499. assertEquals("POST", method.get());
  2500. assertEquals("user", outputParams.get("name"));
  2501. assertEquals("100", outputParams.get("number"));
  2502. }
  2503.  
  2504. /**
  2505. * Verify POST with escaped query parameters
  2506. *
  2507. * @throws Exception
  2508. */
  2509. @Test
  2510. public void postWithEscapedMappedQueryParams() throws Exception {
  2511. Map<String, String> inputParams = new HashMap<String, String>();
  2512. inputParams.put("name", "us er");
  2513. inputParams.put("number", "100");
  2514. final Map<String, String> outputParams = new HashMap<String, String>();
  2515. final AtomicReference<String> method = new AtomicReference<String>();
  2516. handler = new RequestHandler() {
  2517.  
  2518. @Override
  2519. public void handle(Request request, HttpServletResponse response) {
  2520. method.set(request.getMethod());
  2521. outputParams.put("name", request.getParameter("name"));
  2522. outputParams.put("number", request.getParameter("number"));
  2523. response.setStatus(HTTP_OK);
  2524. }
  2525. };
  2526. HttpRequest request = post(url, inputParams, true);
  2527. assertTrue(request.ok());
  2528. assertEquals("POST", method.get());
  2529. assertEquals("us er", outputParams.get("name"));
  2530. assertEquals("100", outputParams.get("number"));
  2531. }
  2532.  
  2533. /**
  2534. * Verify POST with escaped query parameters
  2535. *
  2536. * @throws Exception
  2537. */
  2538. @Test
  2539. public void postWithEscapedVarargsQueryParams() throws Exception {
  2540. final Map<String, String> outputParams = new HashMap<String, String>();
  2541. final AtomicReference<String> method = new AtomicReference<String>();
  2542. handler = new RequestHandler() {
  2543.  
  2544. @Override
  2545. public void handle(Request request, HttpServletResponse response) {
  2546. method.set(request.getMethod());
  2547. outputParams.put("name", request.getParameter("name"));
  2548. outputParams.put("number", request.getParameter("number"));
  2549. response.setStatus(HTTP_OK);
  2550. }
  2551. };
  2552. HttpRequest request = post(url, true, "name", "us er", "number", "100");
  2553. assertTrue(request.ok());
  2554. assertEquals("POST", method.get());
  2555. assertEquals("us er", outputParams.get("name"));
  2556. assertEquals("100", outputParams.get("number"));
  2557. }
  2558.  
  2559. /**
  2560. * Verify POST with numeric query parameters
  2561. *
  2562. * @throws Exception
  2563. */
  2564. @Test
  2565. public void postWithNumericQueryParams() throws Exception {
  2566. Map<Object, Object> inputParams = new HashMap<Object, Object>();
  2567. inputParams.put(1, 2);
  2568. inputParams.put(3, 4);
  2569. final Map<String, String> outputParams = new HashMap<String, String>();
  2570. final AtomicReference<String> method = new AtomicReference<String>();
  2571. handler = new RequestHandler() {
  2572.  
  2573. @Override
  2574. public void handle(Request request, HttpServletResponse response) {
  2575. method.set(request.getMethod());
  2576. outputParams.put("1", request.getParameter("1"));
  2577. outputParams.put("3", request.getParameter("3"));
  2578. response.setStatus(HTTP_OK);
  2579. }
  2580. };
  2581. HttpRequest request = post(url, inputParams, false);
  2582. assertTrue(request.ok());
  2583. assertEquals("POST", method.get());
  2584. assertEquals("2", outputParams.get("1"));
  2585. assertEquals("4", outputParams.get("3"));
  2586. }
  2587.  
  2588. /**
  2589. * Verify GET with query parameters
  2590. *
  2591. * @throws Exception
  2592. */
  2593. @Test
  2594. public void getWithMappedQueryParams() throws Exception {
  2595. Map<String, String> inputParams = new HashMap<String, String>();
  2596. inputParams.put("name", "user");
  2597. inputParams.put("number", "100");
  2598. final Map<String, String> outputParams = new HashMap<String, String>();
  2599. final AtomicReference<String> method = new AtomicReference<String>();
  2600. handler = new RequestHandler() {
  2601.  
  2602. @Override
  2603. public void handle(Request request, HttpServletResponse response) {
  2604. method.set(request.getMethod());
  2605. outputParams.put("name", request.getParameter("name"));
  2606. outputParams.put("number", request.getParameter("number"));
  2607. response.setStatus(HTTP_OK);
  2608. }
  2609. };
  2610. HttpRequest request = get(url, inputParams, false);
  2611. assertTrue(request.ok());
  2612. assertEquals("GET", method.get());
  2613. assertEquals("user", outputParams.get("name"));
  2614. assertEquals("100", outputParams.get("number"));
  2615. }
  2616.  
  2617. /**
  2618. * Verify GET with query parameters
  2619. *
  2620. * @throws Exception
  2621. */
  2622. @Test
  2623. public void getWithVarargsQueryParams() throws Exception {
  2624. final Map<String, String> outputParams = new HashMap<String, String>();
  2625. final AtomicReference<String> method = new AtomicReference<String>();
  2626. handler = new RequestHandler() {
  2627.  
  2628. @Override
  2629. public void handle(Request request, HttpServletResponse response) {
  2630. method.set(request.getMethod());
  2631. outputParams.put("name", request.getParameter("name"));
  2632. outputParams.put("number", request.getParameter("number"));
  2633. response.setStatus(HTTP_OK);
  2634. }
  2635. };
  2636. HttpRequest request = get(url, false, "name", "user", "number", "100");
  2637. assertTrue(request.ok());
  2638. assertEquals("GET", method.get());
  2639. assertEquals("user", outputParams.get("name"));
  2640. assertEquals("100", outputParams.get("number"));
  2641. }
  2642.  
  2643. /**
  2644. * Verify GET with escaped query parameters
  2645. *
  2646. * @throws Exception
  2647. */
  2648. @Test
  2649. public void getWithEscapedMappedQueryParams() throws Exception {
  2650. Map<String, String> inputParams = new HashMap<String, String>();
  2651. inputParams.put("name", "us er");
  2652. inputParams.put("number", "100");
  2653. final Map<String, String> outputParams = new HashMap<String, String>();
  2654. final AtomicReference<String> method = new AtomicReference<String>();
  2655. handler = new RequestHandler() {
  2656.  
  2657. @Override
  2658. public void handle(Request request, HttpServletResponse response) {
  2659. method.set(request.getMethod());
  2660. outputParams.put("name", request.getParameter("name"));
  2661. outputParams.put("number", request.getParameter("number"));
  2662. response.setStatus(HTTP_OK);
  2663. }
  2664. };
  2665. HttpRequest request = get(url, inputParams, true);
  2666. assertTrue(request.ok());
  2667. assertEquals("GET", method.get());
  2668. assertEquals("us er", outputParams.get("name"));
  2669. assertEquals("100", outputParams.get("number"));
  2670. }
  2671.  
  2672. /**
  2673. * Verify GET with escaped query parameters
  2674. *
  2675. * @throws Exception
  2676. */
  2677. @Test
  2678. public void getWithEscapedVarargsQueryParams() throws Exception {
  2679. final Map<String, String> outputParams = new HashMap<String, String>();
  2680. final AtomicReference<String> method = new AtomicReference<String>();
  2681. handler = new RequestHandler() {
  2682.  
  2683. @Override
  2684. public void handle(Request request, HttpServletResponse response) {
  2685. method.set(request.getMethod());
  2686. outputParams.put("name", request.getParameter("name"));
  2687. outputParams.put("number", request.getParameter("number"));
  2688. response.setStatus(HTTP_OK);
  2689. }
  2690. };
  2691. HttpRequest request = get(url, true, "name", "us er", "number", "100");
  2692. assertTrue(request.ok());
  2693. assertEquals("GET", method.get());
  2694. assertEquals("us er", outputParams.get("name"));
  2695. assertEquals("100", outputParams.get("number"));
  2696. }
  2697.  
  2698. /**
  2699. * Verify DELETE with query parameters
  2700. *
  2701. * @throws Exception
  2702. */
  2703. @Test
  2704. public void deleteWithMappedQueryParams() throws Exception {
  2705. Map<String, String> inputParams = new HashMap<String, String>();
  2706. inputParams.put("name", "user");
  2707. inputParams.put("number", "100");
  2708. final Map<String, String> outputParams = new HashMap<String, String>();
  2709. final AtomicReference<String> method = new AtomicReference<String>();
  2710. handler = new RequestHandler() {
  2711.  
  2712. @Override
  2713. public void handle(Request request, HttpServletResponse response) {
  2714. method.set(request.getMethod());
  2715. outputParams.put("name", request.getParameter("name"));
  2716. outputParams.put("number", request.getParameter("number"));
  2717. response.setStatus(HTTP_OK);
  2718. }
  2719. };
  2720. HttpRequest request = delete(url, inputParams, false);
  2721. assertTrue(request.ok());
  2722. assertEquals("DELETE", method.get());
  2723. assertEquals("user", outputParams.get("name"));
  2724. assertEquals("100", outputParams.get("number"));
  2725. }
  2726.  
  2727. /**
  2728. * Verify DELETE with query parameters
  2729. *
  2730. * @throws Exception
  2731. */
  2732. @Test
  2733. public void deleteWithVarargsQueryParams() throws Exception {
  2734. final Map<String, String> outputParams = new HashMap<String, String>();
  2735. final AtomicReference<String> method = new AtomicReference<String>();
  2736. handler = new RequestHandler() {
  2737.  
  2738. @Override
  2739. public void handle(Request request, HttpServletResponse response) {
  2740. method.set(request.getMethod());
  2741. outputParams.put("name", request.getParameter("name"));
  2742. outputParams.put("number", request.getParameter("number"));
  2743. response.setStatus(HTTP_OK);
  2744. }
  2745. };
  2746. HttpRequest request = delete(url, false, "name", "user", "number", "100");
  2747. assertTrue(request.ok());
  2748. assertEquals("DELETE", method.get());
  2749. assertEquals("user", outputParams.get("name"));
  2750. assertEquals("100", outputParams.get("number"));
  2751. }
  2752.  
  2753. /**
  2754. * Verify DELETE with escaped query parameters
  2755. *
  2756. * @throws Exception
  2757. */
  2758. @Test
  2759. public void deleteWithEscapedMappedQueryParams() throws Exception {
  2760. Map<String, String> inputParams = new HashMap<String, String>();
  2761. inputParams.put("name", "us er");
  2762. inputParams.put("number", "100");
  2763. final Map<String, String> outputParams = new HashMap<String, String>();
  2764. final AtomicReference<String> method = new AtomicReference<String>();
  2765. handler = new RequestHandler() {
  2766.  
  2767. @Override
  2768. public void handle(Request request, HttpServletResponse response) {
  2769. method.set(request.getMethod());
  2770. outputParams.put("name", request.getParameter("name"));
  2771. outputParams.put("number", request.getParameter("number"));
  2772. response.setStatus(HTTP_OK);
  2773. }
  2774. };
  2775. HttpRequest request = delete(url, inputParams, true);
  2776. assertTrue(request.ok());
  2777. assertEquals("DELETE", method.get());
  2778. assertEquals("us er", outputParams.get("name"));
  2779. assertEquals("100", outputParams.get("number"));
  2780. }
  2781.  
  2782. /**
  2783. * Verify DELETE with escaped query parameters
  2784. *
  2785. * @throws Exception
  2786. */
  2787. @Test
  2788. public void deleteWithEscapedVarargsQueryParams() throws Exception {
  2789. final Map<String, String> outputParams = new HashMap<String, String>();
  2790. final AtomicReference<String> method = new AtomicReference<String>();
  2791. handler = new RequestHandler() {
  2792.  
  2793. @Override
  2794. public void handle(Request request, HttpServletResponse response) {
  2795. method.set(request.getMethod());
  2796. outputParams.put("name", request.getParameter("name"));
  2797. outputParams.put("number", request.getParameter("number"));
  2798. response.setStatus(HTTP_OK);
  2799. }
  2800. };
  2801. HttpRequest request = delete(url, true, "name", "us er", "number", "100");
  2802. assertTrue(request.ok());
  2803. assertEquals("DELETE", method.get());
  2804. assertEquals("us er", outputParams.get("name"));
  2805. assertEquals("100", outputParams.get("number"));
  2806. }
  2807.  
  2808. /**
  2809. * Verify PUT with query parameters
  2810. *
  2811. * @throws Exception
  2812. */
  2813. @Test
  2814. public void putWithMappedQueryParams() throws Exception {
  2815. Map<String, String> inputParams = new HashMap<String, String>();
  2816. inputParams.put("name", "user");
  2817. inputParams.put("number", "100");
  2818. final Map<String, String> outputParams = new HashMap<String, String>();
  2819. final AtomicReference<String> method = new AtomicReference<String>();
  2820. handler = new RequestHandler() {
  2821.  
  2822. @Override
  2823. public void handle(Request request, HttpServletResponse response) {
  2824. method.set(request.getMethod());
  2825. outputParams.put("name", request.getParameter("name"));
  2826. outputParams.put("number", request.getParameter("number"));
  2827. response.setStatus(HTTP_OK);
  2828. }
  2829. };
  2830. HttpRequest request = put(url, inputParams, false);
  2831. assertTrue(request.ok());
  2832. assertEquals("PUT", method.get());
  2833. assertEquals("user", outputParams.get("name"));
  2834. assertEquals("100", outputParams.get("number"));
  2835. }
  2836.  
  2837. /**
  2838. * Verify PUT with query parameters
  2839. *
  2840. * @throws Exception
  2841. */
  2842. @Test
  2843. public void putWithVarargsQueryParams() throws Exception {
  2844. final Map<String, String> outputParams = new HashMap<String, String>();
  2845. final AtomicReference<String> method = new AtomicReference<String>();
  2846. handler = new RequestHandler() {
  2847.  
  2848. @Override
  2849. public void handle(Request request, HttpServletResponse response) {
  2850. method.set(request.getMethod());
  2851. outputParams.put("name", request.getParameter("name"));
  2852. outputParams.put("number", request.getParameter("number"));
  2853. response.setStatus(HTTP_OK);
  2854. }
  2855. };
  2856. HttpRequest request = put(url, false, "name", "user", "number", "100");
  2857. assertTrue(request.ok());
  2858. assertEquals("PUT", method.get());
  2859. assertEquals("user", outputParams.get("name"));
  2860. assertEquals("100", outputParams.get("number"));
  2861. }
  2862.  
  2863. /**
  2864. * Verify PUT with escaped query parameters
  2865. *
  2866. * @throws Exception
  2867. */
  2868. @Test
  2869. public void putWithEscapedMappedQueryParams() throws Exception {
  2870. Map<String, String> inputParams = new HashMap<String, String>();
  2871. inputParams.put("name", "us er");
  2872. inputParams.put("number", "100");
  2873. final Map<String, String> outputParams = new HashMap<String, String>();
  2874. final AtomicReference<String> method = new AtomicReference<String>();
  2875. handler = new RequestHandler() {
  2876.  
  2877. @Override
  2878. public void handle(Request request, HttpServletResponse response) {
  2879. method.set(request.getMethod());
  2880. outputParams.put("name", request.getParameter("name"));
  2881. outputParams.put("number", request.getParameter("number"));
  2882. response.setStatus(HTTP_OK);
  2883. }
  2884. };
  2885. HttpRequest request = put(url, inputParams, true);
  2886. assertTrue(request.ok());
  2887. assertEquals("PUT", method.get());
  2888. assertEquals("us er", outputParams.get("name"));
  2889. assertEquals("100", outputParams.get("number"));
  2890. }
  2891.  
  2892. /**
  2893. * Verify PUT with escaped query parameters
  2894. *
  2895. * @throws Exception
  2896. */
  2897. @Test
  2898. public void putWithEscapedVarargsQueryParams() throws Exception {
  2899. final Map<String, String> outputParams = new HashMap<String, String>();
  2900. final AtomicReference<String> method = new AtomicReference<String>();
  2901. handler = new RequestHandler() {
  2902.  
  2903. @Override
  2904. public void handle(Request request, HttpServletResponse response) {
  2905. method.set(request.getMethod());
  2906. outputParams.put("name", request.getParameter("name"));
  2907. outputParams.put("number", request.getParameter("number"));
  2908. response.setStatus(HTTP_OK);
  2909. }
  2910. };
  2911. HttpRequest request = put(url, true, "name", "us er", "number", "100");
  2912. assertTrue(request.ok());
  2913. assertEquals("PUT", method.get());
  2914. assertEquals("us er", outputParams.get("name"));
  2915. assertEquals("100", outputParams.get("number"));
  2916. }
  2917.  
  2918. /**
  2919. * Verify HEAD with query parameters
  2920. *
  2921. * @throws Exception
  2922. */
  2923. @Test
  2924. public void headWithMappedQueryParams() throws Exception {
  2925. Map<String, String> inputParams = new HashMap<String, String>();
  2926. inputParams.put("name", "user");
  2927. inputParams.put("number", "100");
  2928. final Map<String, String> outputParams = new HashMap<String, String>();
  2929. final AtomicReference<String> method = new AtomicReference<String>();
  2930. handler = new RequestHandler() {
  2931.  
  2932. @Override
  2933. public void handle(Request request, HttpServletResponse response) {
  2934. method.set(request.getMethod());
  2935. outputParams.put("name", request.getParameter("name"));
  2936. outputParams.put("number", request.getParameter("number"));
  2937. response.setStatus(HTTP_OK);
  2938. }
  2939. };
  2940. HttpRequest request = head(url, inputParams, false);
  2941. assertTrue(request.ok());
  2942. assertEquals("HEAD", method.get());
  2943. assertEquals("user", outputParams.get("name"));
  2944. assertEquals("100", outputParams.get("number"));
  2945. }
  2946.  
  2947. /**
  2948. * Verify HEAD with query parameters
  2949. *
  2950. * @throws Exception
  2951. */
  2952. @Test
  2953. public void headWithVaragsQueryParams() throws Exception {
  2954. final Map<String, String> outputParams = new HashMap<String, String>();
  2955. final AtomicReference<String> method = new AtomicReference<String>();
  2956. handler = new RequestHandler() {
  2957.  
  2958. @Override
  2959. public void handle(Request request, HttpServletResponse response) {
  2960. method.set(request.getMethod());
  2961. outputParams.put("name", request.getParameter("name"));
  2962. outputParams.put("number", request.getParameter("number"));
  2963. response.setStatus(HTTP_OK);
  2964. }
  2965. };
  2966. HttpRequest request = head(url, false, "name", "user", "number", "100");
  2967. assertTrue(request.ok());
  2968. assertEquals("HEAD", method.get());
  2969. assertEquals("user", outputParams.get("name"));
  2970. assertEquals("100", outputParams.get("number"));
  2971. }
  2972.  
  2973. /**
  2974. * Verify HEAD with escaped query parameters
  2975. *
  2976. * @throws Exception
  2977. */
  2978. @Test
  2979. public void headWithEscapedMappedQueryParams() throws Exception {
  2980. Map<String, String> inputParams = new HashMap<String, String>();
  2981. inputParams.put("name", "us er");
  2982. inputParams.put("number", "100");
  2983. final Map<String, String> outputParams = new HashMap<String, String>();
  2984. final AtomicReference<String> method = new AtomicReference<String>();
  2985. handler = new RequestHandler() {
  2986.  
  2987. @Override
  2988. public void handle(Request request, HttpServletResponse response) {
  2989. method.set(request.getMethod());
  2990. outputParams.put("name", request.getParameter("name"));
  2991. outputParams.put("number", request.getParameter("number"));
  2992. response.setStatus(HTTP_OK);
  2993. }
  2994. };
  2995. HttpRequest request = head(url, inputParams, true);
  2996. assertTrue(request.ok());
  2997. assertEquals("HEAD", method.get());
  2998. assertEquals("us er", outputParams.get("name"));
  2999. assertEquals("100", outputParams.get("number"));
  3000. }
  3001.  
  3002. /**
  3003. * Verify HEAD with escaped query parameters
  3004. *
  3005. * @throws Exception
  3006. */
  3007. @Test
  3008. public void headWithEscapedVarargsQueryParams() throws Exception {
  3009. final Map<String, String> outputParams = new HashMap<String, String>();
  3010. final AtomicReference<String> method = new AtomicReference<String>();
  3011. handler = new RequestHandler() {
  3012.  
  3013. @Override
  3014. public void handle(Request request, HttpServletResponse response) {
  3015. method.set(request.getMethod());
  3016. outputParams.put("name", request.getParameter("name"));
  3017. outputParams.put("number", request.getParameter("number"));
  3018. response.setStatus(HTTP_OK);
  3019. }
  3020. };
  3021. HttpRequest request = head(url, true, "name", "us er", "number", "100");
  3022. assertTrue(request.ok());
  3023. assertEquals("HEAD", method.get());
  3024. assertEquals("us er", outputParams.get("name"));
  3025. assertEquals("100", outputParams.get("number"));
  3026. }
  3027.  
  3028. /**
  3029. * Append with base URL with no path
  3030. *
  3031. * @throws Exception
  3032. */
  3033. @Test
  3034. public void appendMappedQueryParamsWithNoPath() throws Exception {
  3035. assertEquals(
  3036. "http://test.com/?a=b",
  3037. HttpRequest.append("http://test.com",
  3038. Collections.singletonMap("a", "b")));
  3039. }
  3040.  
  3041. /**
  3042. * Append with base URL with no path
  3043. *
  3044. * @throws Exception
  3045. */
  3046. @Test
  3047. public void appendVarargsQueryParmasWithNoPath() throws Exception {
  3048. assertEquals("http://test.com/?a=b",
  3049. HttpRequest.append("http://test.com", "a", "b"));
  3050. }
  3051.  
  3052. /**
  3053. * Append with base URL with path
  3054. *
  3055. * @throws Exception
  3056. */
  3057. @Test
  3058. public void appendMappedQueryParamsWithPath() throws Exception {
  3059. assertEquals(
  3060. "http://test.com/segment1?a=b",
  3061. HttpRequest.append("http://test.com/segment1",
  3062. Collections.singletonMap("a", "b")));
  3063. assertEquals(
  3064. "http://test.com/?a=b",
  3065. HttpRequest.append("http://test.com/",
  3066. Collections.singletonMap("a", "b")));
  3067. }
  3068.  
  3069. /**
  3070. * Append with base URL with path
  3071. *
  3072. * @throws Exception
  3073. */
  3074. @Test
  3075. public void appendVarargsQueryParamsWithPath() throws Exception {
  3076. assertEquals("http://test.com/segment1?a=b",
  3077. HttpRequest.append("http://test.com/segment1", "a", "b"));
  3078. assertEquals("http://test.com/?a=b",
  3079. HttpRequest.append("http://test.com/", "a", "b"));
  3080. }
  3081.  
  3082. /**
  3083. * Append multiple params
  3084. *
  3085. * @throws Exception
  3086. */
  3087. @Test
  3088. public void appendMultipleMappedQueryParams() throws Exception {
  3089. Map<String, Object> params = new LinkedHashMap<String, Object>();
  3090. params.put("a", "b");
  3091. params.put("c", "d");
  3092. assertEquals("http://test.com/1?a=b&c=d",
  3093. HttpRequest.append("http://test.com/1", params));
  3094. }
  3095.  
  3096. /**
  3097. * Append multiple params
  3098. *
  3099. * @throws Exception
  3100. */
  3101. @Test
  3102. public void appendMultipleVarargsQueryParams() throws Exception {
  3103. assertEquals("http://test.com/1?a=b&c=d",
  3104. HttpRequest.append("http://test.com/1", "a", "b", "c", "d"));
  3105. }
  3106.  
  3107. /**
  3108. * Append null params
  3109. *
  3110. * @throws Exception
  3111. */
  3112. @Test
  3113. public void appendNullMappedQueryParams() throws Exception {
  3114. assertEquals("http://test.com/1",
  3115. HttpRequest.append("http://test.com/1", (Map<?, ?>) null));
  3116. }
  3117.  
  3118. /**
  3119. * Append null params
  3120. *
  3121. * @throws Exception
  3122. */
  3123. @Test
  3124. public void appendNullVaragsQueryParams() throws Exception {
  3125. assertEquals("http://test.com/1",
  3126. HttpRequest.append("http://test.com/1", (Object[]) null));
  3127. }
  3128.  
  3129. /**
  3130. * Append empty params
  3131. *
  3132. * @throws Exception
  3133. */
  3134. @Test
  3135. public void appendEmptyMappedQueryParams() throws Exception {
  3136. assertEquals(
  3137. "http://test.com/1",
  3138. HttpRequest.append("http://test.com/1",
  3139. Collections.<String, String> emptyMap()));
  3140. }
  3141.  
  3142. /**
  3143. * Append empty params
  3144. *
  3145. * @throws Exception
  3146. */
  3147. @Test
  3148. public void appendEmptyVarargsQueryParams() throws Exception {
  3149. assertEquals("http://test.com/1",
  3150. HttpRequest.append("http://test.com/1", new Object[0]));
  3151. }
  3152.  
  3153. /**
  3154. * Append params with null values
  3155. *
  3156. * @throws Exception
  3157. */
  3158. @Test
  3159. public void appendWithNullMappedQueryParamValues() throws Exception {
  3160. Map<String, Object> params = new LinkedHashMap<String, Object>();
  3161. params.put("a", null);
  3162. params.put("b", null);
  3163. assertEquals("http://test.com/1?a=&b=",
  3164. HttpRequest.append("http://test.com/1", params));
  3165. }
  3166.  
  3167. /**
  3168. * Append params with null values
  3169. *
  3170. * @throws Exception
  3171. */
  3172. @Test
  3173. public void appendWithNullVaragsQueryParamValues() throws Exception {
  3174. assertEquals("http://test.com/1?a=&b=",
  3175. HttpRequest.append("http://test.com/1", "a", null, "b", null));
  3176. }
  3177.  
  3178. /**
  3179. * Try to append with wrong number of arguments
  3180. */
  3181. @Test(expected = IllegalArgumentException.class)
  3182. public void appendOddNumberOfParams() {
  3183. HttpRequest.append("http://test.com", "1");
  3184. }
  3185.  
  3186. /**
  3187. * Append with base URL already containing a '?'
  3188. */
  3189. @Test
  3190. public void appendMappedQueryParamsWithExistingQueryStart() {
  3191. assertEquals(
  3192. "http://test.com/1?a=b",
  3193. HttpRequest.append("http://test.com/1?",
  3194. Collections.singletonMap("a", "b")));
  3195. }
  3196.  
  3197. /**
  3198. * Append with base URL already containing a '?'
  3199. */
  3200. @Test
  3201. public void appendVarargsQueryParamsWithExistingQueryStart() {
  3202. assertEquals("http://test.com/1?a=b",
  3203. HttpRequest.append("http://test.com/1?", "a", "b"));
  3204. }
  3205.  
  3206. /**
  3207. * Append with base URL already containing a '?'
  3208. */
  3209. @Test
  3210. public void appendMappedQueryParamsWithExistingParams() {
  3211. assertEquals(
  3212. "http://test.com/1?a=b&c=d",
  3213. HttpRequest.append("http://test.com/1?a=b",
  3214. Collections.singletonMap("c", "d")));
  3215. assertEquals(
  3216. "http://test.com/1?a=b&c=d",
  3217. HttpRequest.append("http://test.com/1?a=b&",
  3218. Collections.singletonMap("c", "d")));
  3219.  
  3220. }
  3221.  
  3222. /**
  3223. * Append with base URL already containing a '?'
  3224. */
  3225. @Test
  3226. public void appendWithVarargsQueryParamsWithExistingParams() {
  3227. assertEquals("http://test.com/1?a=b&c=d",
  3228. HttpRequest.append("http://test.com/1?a=b", "c", "d"));
  3229. assertEquals("http://test.com/1?a=b&c=d",
  3230. HttpRequest.append("http://test.com/1?a=b&", "c", "d"));
  3231. }
  3232.  
  3233. /**
  3234. * Append array parameter
  3235. *
  3236. * @throws Exception
  3237. */
  3238. @Test
  3239. public void appendArrayQueryParams() throws Exception {
  3240. assertEquals(
  3241. "http://test.com/?foo[]=bar&foo[]=baz",
  3242. HttpRequest.append("http://test.com",
  3243. Collections.singletonMap("foo", new String[] { "bar", "baz" })));
  3244. assertEquals(
  3245. "http://test.com/?a[]=1&a[]=2",
  3246. HttpRequest.append("http://test.com",
  3247. Collections.singletonMap("a", new int[] { 1, 2 })));
  3248. assertEquals(
  3249. "http://test.com/?a[]=1",
  3250. HttpRequest.append("http://test.com",
  3251. Collections.singletonMap("a", new int[] { 1 })));
  3252. assertEquals(
  3253. "http://test.com/?",
  3254. HttpRequest.append("http://test.com",
  3255. Collections.singletonMap("a", new int[] { })));
  3256. assertEquals(
  3257. "http://test.com/?foo[]=bar&foo[]=baz&a[]=1&a[]=2",
  3258. HttpRequest.append("http://test.com",
  3259. "foo", new String[] { "bar", "baz" },
  3260. "a", new int[] { 1, 2 }));
  3261. }
  3262.  
  3263. /**
  3264. * Append list parameter
  3265. *
  3266. * @throws Exception
  3267. */
  3268. @Test
  3269. public void appendListQueryParams() throws Exception {
  3270. assertEquals(
  3271. "http://test.com/?foo[]=bar&foo[]=baz",
  3272. HttpRequest.append("http://test.com",
  3273. Collections.singletonMap("foo", Arrays.asList(new String[] { "bar", "baz" }))));
  3274. assertEquals(
  3275. "http://test.com/?a[]=1&a[]=2",
  3276. HttpRequest.append("http://test.com",
  3277. Collections.singletonMap("a", Arrays.asList(new Integer[] { 1, 2 }))));
  3278. assertEquals(
  3279. "http://test.com/?a[]=1",
  3280. HttpRequest.append("http://test.com",
  3281. Collections.singletonMap("a", Arrays.asList(new Integer[] { 1 }))));
  3282. assertEquals(
  3283. "http://test.com/?",
  3284. HttpRequest.append("http://test.com",
  3285. Collections.singletonMap("a", Arrays.asList(new Integer[] { }))));
  3286. assertEquals(
  3287. "http://test.com/?foo[]=bar&foo[]=baz&a[]=1&a[]=2",
  3288. HttpRequest.append("http://test.com",
  3289. "foo", Arrays.asList(new String[] { "bar", "baz" }),
  3290. "a", Arrays.asList(new Integer[] { 1, 2 })));
  3291. }
  3292.  
  3293. /**
  3294. * Get a 500
  3295. *
  3296. * @throws Exception
  3297. */
  3298. @Test
  3299. public void serverErrorCode() throws Exception {
  3300. handler = new RequestHandler() {
  3301.  
  3302. @Override
  3303. public void handle(Request request, HttpServletResponse response) {
  3304. response.setStatus(HTTP_INTERNAL_ERROR);
  3305. }
  3306. };
  3307. HttpRequest request = get(url);
  3308. assertNotNull(request);
  3309. assertTrue(request.serverError());
  3310. }
  3311.  
  3312. /**
  3313. * Get a 400
  3314. *
  3315. * @throws Exception
  3316. */
  3317. @Test
  3318. public void badRequestCode() throws Exception {
  3319. handler = new RequestHandler() {
  3320.  
  3321. @Override
  3322. public void handle(Request request, HttpServletResponse response) {
  3323. response.setStatus(HTTP_BAD_REQUEST);
  3324. }
  3325. };
  3326. HttpRequest request = get(url);
  3327. assertNotNull(request);
  3328. assertTrue(request.badRequest());
  3329. }
  3330.  
  3331. /**
  3332. * Get a 304
  3333. *
  3334. * @throws Exception
  3335. */
  3336. @Test
  3337. public void notModifiedCode() throws Exception {
  3338. handler = new RequestHandler() {
  3339.  
  3340. @Override
  3341. public void handle(Request request, HttpServletResponse response) {
  3342. response.setStatus(HTTP_NOT_MODIFIED);
  3343. }
  3344. };
  3345. HttpRequest request = get(url);
  3346. assertNotNull(request);
  3347. assertTrue(request.notModified());
  3348. }
  3349.  
  3350. /**
  3351. * Verify data is sent when receiving response without first calling
  3352. * {@link HttpRequest#code()}
  3353. *
  3354. * @throws Exception
  3355. */
  3356. @Test
  3357. public void sendReceiveWithoutCode() throws Exception {
  3358. final AtomicReference<String> body = new AtomicReference<String>();
  3359. handler = new RequestHandler() {
  3360.  
  3361. @Override
  3362. public void handle(Request request, HttpServletResponse response) {
  3363. body.set(new String(read()));
  3364. try {
  3365. response.getWriter().write("world");
  3366. } catch (IOException ignored) {
  3367. // Ignored
  3368. }
  3369. response.setStatus(HTTP_OK);
  3370. }
  3371. };
  3372.  
  3373. HttpRequest request = post(url).ignoreCloseExceptions(false);
  3374. assertEquals("world", request.send("hello").body());
  3375. assertEquals("hello", body.get());
  3376. }
  3377.  
  3378. /**
  3379. * Verify data is send when receiving response headers without first calling
  3380. * {@link HttpRequest#code()}
  3381. *
  3382. * @throws Exception
  3383. */
  3384. @Test
  3385. public void sendHeadersWithoutCode() throws Exception {
  3386. final AtomicReference<String> body = new AtomicReference<String>();
  3387. handler = new RequestHandler() {
  3388.  
  3389. @Override
  3390. public void handle(Request request, HttpServletResponse response) {
  3391. body.set(new String(read()));
  3392. response.setHeader("h1", "v1");
  3393. response.setHeader("h2", "v2");
  3394. response.setStatus(HTTP_OK);
  3395. }
  3396. };
  3397.  
  3398. HttpRequest request = post(url).ignoreCloseExceptions(false);
  3399. Map<String, List<String>> headers = request.send("hello").headers();
  3400. assertEquals("v1", headers.get("h1").get(0));
  3401. assertEquals("v2", headers.get("h2").get(0));
  3402. assertEquals("hello", body.get());
  3403. }
  3404.  
  3405. /**
  3406. * Verify data is send when receiving response date header without first
  3407. * calling {@link HttpRequest#code()}
  3408. *
  3409. * @throws Exception
  3410. */
  3411. @Test
  3412. public void sendDateHeaderWithoutCode() throws Exception {
  3413. final AtomicReference<String> body = new AtomicReference<String>();
  3414. handler = new RequestHandler() {
  3415.  
  3416. @Override
  3417. public void handle(Request request, HttpServletResponse response) {
  3418. body.set(new String(read()));
  3419. response.setDateHeader("Date", 1000);
  3420. response.setStatus(HTTP_OK);
  3421. }
  3422. };
  3423.  
  3424. HttpRequest request = post(url).ignoreCloseExceptions(false);
  3425. assertEquals(1000, request.send("hello").date());
  3426. assertEquals("hello", body.get());
  3427. }
  3428.  
  3429. /**
  3430. * Verify data is send when receiving response integer header without first
  3431. * calling {@link HttpRequest#code()}
  3432. *
  3433. * @throws Exception
  3434. */
  3435. @Test
  3436. public void sendIntHeaderWithoutCode() throws Exception {
  3437. final AtomicReference<String> body = new AtomicReference<String>();
  3438. handler = new RequestHandler() {
  3439.  
  3440. @Override
  3441. public void handle(Request request, HttpServletResponse response) {
  3442. body.set(new String(read()));
  3443. response.setIntHeader("Width", 9876);
  3444. response.setStatus(HTTP_OK);
  3445. }
  3446. };
  3447.  
  3448. HttpRequest request = post(url).ignoreCloseExceptions(false);
  3449. assertEquals(9876, request.send("hello").intHeader("Width"));
  3450. assertEquals("hello", body.get());
  3451. }
  3452.  
  3453. /**
  3454. * Verify custom connection factory
  3455. */
  3456. @Test
  3457. public void customConnectionFactory() throws Exception {
  3458. handler = new RequestHandler() {
  3459.  
  3460. @Override
  3461. public void handle(Request request, HttpServletResponse response) {
  3462. response.setStatus(HTTP_OK);
  3463. }
  3464. };
  3465.  
  3466. ConnectionFactory factory = new ConnectionFactory() {
  3467.  
  3468. public HttpURLConnection create(URL otherUrl) throws IOException {
  3469. return (HttpURLConnection) new URL(url).openConnection();
  3470. }
  3471.  
  3472. public HttpURLConnection create(URL url, Proxy proxy) throws IOException {
  3473. throw new IOException();
  3474. }
  3475. };
  3476.  
  3477. HttpRequest.setConnectionFactory(factory);
  3478. int code = get("http://not/a/real/url").code();
  3479. assertEquals(200, code);
  3480. }
  3481.  
  3482. /**
  3483. * Verify setting a null connection factory restores to the default one
  3484. */
  3485. @Test
  3486. public void nullConnectionFactory() throws Exception {
  3487. handler = new RequestHandler() {
  3488.  
  3489. @Override
  3490. public void handle(Request request, HttpServletResponse response) {
  3491. response.setStatus(HTTP_OK);
  3492. }
  3493. };
  3494.  
  3495. HttpRequest.setConnectionFactory(null);
  3496. int code = get(url).code();
  3497. assertEquals(200, code);
  3498. }
  3499.  
  3500. /**
  3501. * Verify reading response body for empty 200
  3502. *
  3503. * @throws Exception
  3504. */
  3505. @Test
  3506. public void streamOfEmptyOkResponse() throws Exception {
  3507. handler = new RequestHandler() {
  3508.  
  3509. @Override
  3510. public void handle(Request request, HttpServletResponse response) {
  3511. response.setStatus(200);
  3512. }
  3513. };
  3514. assertEquals("", get(url).body());
  3515. }
  3516.  
  3517. /**
  3518. * Verify reading response body for empty 400
  3519. *
  3520. * @throws Exception
  3521. */
  3522. @Test
  3523. public void bodyOfEmptyErrorResponse() throws Exception {
  3524. handler = new RequestHandler() {
  3525.  
  3526. @Override
  3527. public void handle(Request request, HttpServletResponse response) {
  3528. response.setStatus(HTTP_BAD_REQUEST);
  3529. }
  3530. };
  3531. assertEquals("", get(url).body());
  3532. }
  3533.  
  3534. /**
  3535. * Verify reading response body for non-empty 400
  3536. *
  3537. * @throws Exception
  3538. */
  3539. @Test
  3540. public void bodyOfNonEmptyErrorResponse() throws Exception {
  3541. handler = new RequestHandler() {
  3542.  
  3543. @Override
  3544. public void handle(Request request, HttpServletResponse response) {
  3545. response.setStatus(HTTP_BAD_REQUEST);
  3546. try {
  3547. response.getWriter().write("error");
  3548. } catch (IOException ignored) {
  3549. // Ignored
  3550. }
  3551. }
  3552. };
  3553. assertEquals("error", get(url).body());
  3554. }
  3555.  
  3556. /**
  3557. * Verify progress callback when sending a file
  3558. *
  3559. * @throws Exception
  3560. */
  3561. @Test
  3562. public void uploadProgressSend() throws Exception {
  3563. final AtomicReference<String> body = new AtomicReference<String>();
  3564. handler = new RequestHandler() {
  3565.  
  3566. @Override
  3567. public void handle(Request request, HttpServletResponse response) {
  3568. body.set(new String(read()));
  3569. response.setStatus(HTTP_OK);
  3570. }
  3571. };
  3572. final File file = File.createTempFile("post", ".txt");
  3573. new FileWriter(file).append("hello").close();
  3574.  
  3575. final AtomicLong tx = new AtomicLong(0);
  3576. UploadProgress progress = new UploadProgress() {
  3577. public void onUpload(long transferred, long total) {
  3578. assertEquals(file.length(), total);
  3579. assertEquals(tx.incrementAndGet(), transferred);
  3580. }
  3581. };
  3582. post(url).bufferSize(1).progress(progress).send(file).code();
  3583. assertEquals(file.length(), tx.get());
  3584. }
  3585.  
  3586. /**
  3587. * Verify progress callback when sending from an InputStream
  3588. *
  3589. * @throws Exception
  3590. */
  3591. @Test
  3592. public void uploadProgressSendInputStream() throws Exception {
  3593. final AtomicReference<String> body = new AtomicReference<String>();
  3594. handler = new RequestHandler() {
  3595.  
  3596. @Override
  3597. public void handle(Request request, HttpServletResponse response) {
  3598. body.set(new String(read()));
  3599. response.setStatus(HTTP_OK);
  3600. }
  3601. };
  3602. File file = File.createTempFile("post", ".txt");
  3603. new FileWriter(file).append("hello").close();
  3604. InputStream input = new FileInputStream(file);
  3605. final AtomicLong tx = new AtomicLong(0);
  3606. UploadProgress progress = new UploadProgress() {
  3607. public void onUpload(long transferred, long total) {
  3608. assertEquals(-1, total);
  3609. assertEquals(tx.incrementAndGet(), transferred);
  3610. }
  3611. };
  3612. post(url).bufferSize(1).progress(progress).send(input).code();
  3613. assertEquals(file.length(), tx.get());
  3614. }
  3615.  
  3616. /**
  3617. * Verify progress callback when sending from a byte array
  3618. *
  3619. * @throws Exception
  3620. */
  3621. @Test
  3622. public void uploadProgressSendByteArray() throws Exception {
  3623. final AtomicReference<String> body = new AtomicReference<String>();
  3624. handler = new RequestHandler() {
  3625.  
  3626. @Override
  3627. public void handle(Request request, HttpServletResponse response) {
  3628. body.set(new String(read()));
  3629. response.setStatus(HTTP_OK);
  3630. }
  3631. };
  3632.  
  3633. final byte[] bytes = "hello".getBytes(CHARSET_UTF8);
  3634. final AtomicLong tx = new AtomicLong(0);
  3635. UploadProgress progress = new UploadProgress() {
  3636. public void onUpload(long transferred, long total) {
  3637. assertEquals(bytes.length, total);
  3638. assertEquals(tx.incrementAndGet(), transferred);
  3639. }
  3640. };
  3641. post(url).bufferSize(1).progress(progress).send(bytes).code();
  3642. assertEquals(bytes.length, tx.get());
  3643. }
  3644.  
  3645. /**
  3646. * Verify progress callback when sending from a Reader
  3647. *
  3648. * @throws Exception
  3649. */
  3650. @Test
  3651. public void uploadProgressSendReader() throws Exception {
  3652. final AtomicReference<String> body = new AtomicReference<String>();
  3653. handler = new RequestHandler() {
  3654.  
  3655. @Override
  3656. public void handle(Request request, HttpServletResponse response) {
  3657. body.set(new String(read()));
  3658. response.setStatus(HTTP_OK);
  3659. }
  3660. };
  3661.  
  3662. final AtomicLong tx = new AtomicLong(0);
  3663. UploadProgress progress = new UploadProgress() {
  3664. public void onUpload(long transferred, long total) {
  3665. assertEquals(-1, total);
  3666. assertEquals(tx.incrementAndGet(), transferred);
  3667. }
  3668. };
  3669. File file = File.createTempFile("post", ".txt");
  3670. new FileWriter(file).append("hello").close();
  3671. post(url).progress(progress).bufferSize(1).send(new FileReader(file)).code();
  3672. assertEquals(file.length(), tx.get());
  3673. }
  3674.  
  3675. /**
  3676. * Verify progress callback doesn't cause an exception when it's null
  3677. *
  3678. * @throws Exception
  3679. */
  3680. @Test
  3681. public void nullUploadProgress() throws Exception {
  3682. final AtomicReference<String> body = new AtomicReference<String>();
  3683. handler = new RequestHandler() {
  3684.  
  3685. @Override
  3686. public void handle(Request request, HttpServletResponse response) {
  3687. body.set(new String(read()));
  3688. response.setStatus(HTTP_OK);
  3689. }
  3690. };
  3691. File file = File.createTempFile("post", ".txt");
  3692. new FileWriter(file).append("hello").close();
  3693. int code = post(url).progress(null).send(file).code();
  3694. assertEquals(HTTP_OK, code);
  3695. assertEquals("hello", body.get());
  3696. }
  3697. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement