1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package com.gargoylesoftware.htmlunit.attachment;
16
17 import java.io.ByteArrayInputStream;
18 import java.io.InputStream;
19 import java.util.ArrayList;
20 import java.util.List;
21
22 import org.junit.Test;
23 import org.junit.runner.RunWith;
24
25 import com.gargoylesoftware.htmlunit.BrowserRunner;
26 import com.gargoylesoftware.htmlunit.HttpWebConnectionTest;
27 import com.gargoylesoftware.htmlunit.MockWebConnection;
28 import com.gargoylesoftware.htmlunit.Page;
29 import com.gargoylesoftware.htmlunit.TextPage;
30 import com.gargoylesoftware.htmlunit.WebClient;
31 import com.gargoylesoftware.htmlunit.WebResponse;
32 import com.gargoylesoftware.htmlunit.WebTestCase;
33 import com.gargoylesoftware.htmlunit.html.HtmlAnchor;
34 import com.gargoylesoftware.htmlunit.html.HtmlPage;
35 import com.gargoylesoftware.htmlunit.util.NameValuePair;
36
37
38
39
40
41
42
43
44
45 @RunWith(BrowserRunner.class)
46 public class AttachmentTest extends WebTestCase {
47
48
49
50
51
52 @Test
53 public void basic() throws Exception {
54 final String content1 = "<html><body>\n"
55 + "<form method='POST' name='form' action='" + URL_SECOND + "'>\n"
56 + "<input type='submit' value='ok'>\n"
57 + "</form>\n"
58 + "<a href='#' onclick='document.form.submit()'>click me</a>\n"
59 + "</body></html>";
60 final String content2 = "download file contents";
61
62 final WebClient client = getWebClient();
63 final List<Attachment> attachments = new ArrayList<Attachment>();
64 client.setAttachmentHandler(new CollectingAttachmentHandler(attachments));
65
66 final List<NameValuePair> headers = new ArrayList<NameValuePair>();
67 headers.add(new NameValuePair("Content-Disposition", "attachment"));
68
69 final MockWebConnection conn = new MockWebConnection();
70 conn.setResponse(URL_FIRST, content1);
71 conn.setResponse(URL_SECOND, content2, 200, "OK", "text/html", headers);
72 client.setWebConnection(conn);
73 assertEquals(0, attachments.size());
74
75 final HtmlPage result = client.getPage(URL_FIRST);
76 final HtmlAnchor anchor = result.getAnchors().get(0);
77 final Page clickResult = anchor.click();
78 assertEquals(result, clickResult);
79 assertEquals(1, attachments.size());
80 assertTrue(HtmlPage.class.isInstance(attachments.get(0).getPage()));
81
82 final Attachment attachment = attachments.get(0);
83 final Page attachedPage = attachment.getPage();
84 final WebResponse attachmentResponse = attachedPage.getWebResponse();
85 final InputStream attachmentStream = attachmentResponse.getContentAsStream();
86 HttpWebConnectionTest.assertEquals(new ByteArrayInputStream(content2.getBytes()), attachmentStream);
87 assertEquals("text/html", attachmentResponse.getContentType());
88 assertEquals(200, attachmentResponse.getStatusCode());
89 assertEquals(URL_SECOND, attachmentResponse.getWebRequest().getUrl());
90 }
91
92
93
94
95
96 @Test
97 public void filename() throws Exception {
98 final String content = "<html>But is it really?</html>";
99
100 final WebClient client = getWebClient();
101 final MockWebConnection conn = new MockWebConnection();
102 client.setWebConnection(conn);
103 final List<Attachment> attachments = new ArrayList<Attachment>();
104 client.setAttachmentHandler(new CollectingAttachmentHandler(attachments));
105
106 final List<NameValuePair> headers1 = new ArrayList<NameValuePair>();
107 headers1.add(new NameValuePair("Content-Disposition", "attachment;filename=\"hello.html\""));
108 conn.setResponse(URL_FIRST, content, 200, "OK", "text/html", headers1);
109 client.getPage(URL_FIRST);
110 final Attachment result = attachments.get(0);
111 assertEquals(result.getSuggestedFilename(), "hello.html");
112 attachments.clear();
113
114 final List<NameValuePair> headers2 = new ArrayList<NameValuePair>();
115 headers2.add(new NameValuePair("Content-Disposition", "attachment; filename=hello2.html; something=else"));
116 conn.setResponse(URL_SECOND, content, 200, "OK", "text/plain", headers2);
117 client.getPage(URL_SECOND);
118 final Attachment result2 = attachments.get(0);
119 assertEquals(result2.getSuggestedFilename(), "hello2.html");
120 assertEquals(content, ((TextPage) result2.getPage()).getContent());
121 attachments.clear();
122
123 final List<NameValuePair> headers3 = new ArrayList<NameValuePair>();
124 headers3.add(new NameValuePair("Content-Disposition", "attachment"));
125 final byte[] contentb = new byte[] {(byte) 0xCA, (byte) 0xFE, (byte) 0xBA, (byte) 0xBE};
126 conn.setResponse(URL_THIRD, contentb, 200, "OK", "application/x-rubbish", headers3);
127 client.getPage(URL_THIRD);
128 final Attachment result3 = attachments.get(0);
129 final InputStream result3Stream = result3.getPage().getWebResponse().getContentAsStream();
130 assertNull(result3.getSuggestedFilename());
131 assertEquals(result3.getPage().getWebResponse().getContentType(), "application/x-rubbish");
132 HttpWebConnectionTest.assertEquals(new ByteArrayInputStream(contentb), result3Stream);
133 attachments.clear();
134 }
135
136 }