Skip to content

Commit 197db64

Browse files
committed
Build testing up to verify #23.
com.gistlabs.mechanize.form.FormTest.testFileUploadWithNoFile() didn't fail when I expected it to.
1 parent c3f2538 commit 197db64

File tree

4 files changed

+89
-16
lines changed

4 files changed

+89
-16
lines changed

src/main/java/com/gistlabs/mechanize/MechanizeAgent.java

+11-3
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,9 @@ public Cookies cookies() {
154154
}
155155

156156
/** Returns the content received as a byte buffer.
157+
*
158+
* TODO: Move body into util somewhere
159+
*
157160
* @throws IllegalArgumentException If file exists. */
158161
public byte [] getToBuffer(String uri) {
159162
InputStream content = null;
@@ -211,6 +214,9 @@ public BufferedImage getImage(String uri) {
211214

212215
/** Writes the content received to file.
213216
* This method has a very small memory footprint by using a 4KB buffer.
217+
*
218+
* TODO move body into util somewhere
219+
*
214220
* @throws IllegalArgumentException If file exists. */
215221
public void getToFile(String uri, File file) {
216222
if(file.exists())
@@ -314,11 +320,13 @@ public Page click(Page page, Element link) {
314320
/** Returns the page object received as response to the form submit action. */
315321
public Page submit(Form form, Parameters parameters) {
316322
RequestBuilder request = doRequest(form.getUri()).set(parameters);
317-
if(form.isDoPost() && form.isMultiPart()) {
323+
boolean doPost = form.isDoPost();
324+
boolean multiPart = form.isMultiPart();
325+
if(doPost && multiPart) {
318326
request.multiPart();
319327
addFiles(request, form);
320328
}
321-
return form.isDoPost() ? request.post() : request.get();
329+
return doPost ? request.post() : request.get();
322330
}
323331

324332
private void addFiles(RequestBuilder request, Form form) {
@@ -378,7 +386,7 @@ public RequestBuilder add(Parameters parameters) {
378386
/** Adds a file to the request also making the request to become a multi-part post request or removes any file registered
379387
* under the given name if the file value is null. */
380388
public RequestBuilder set(String name, File file) {
381-
return set(name, (ContentBody) file != null ? new FileBody(file) : null);
389+
return set(name, file != null ? new FileBody(file) : null);
382390
}
383391

384392
/** Adds an ContentBody object. */

src/test/java/com/gistlabs/mechanize/MechanizeTestCase.java

+39-11
Original file line numberDiff line numberDiff line change
@@ -58,24 +58,47 @@ protected FormBuilder newForm(String action) {
5858
return new FormBuilder(action);
5959
}
6060

61-
protected FormBuilder newForm(String action, boolean doPost) {
62-
return new FormBuilder(action, doPost);
61+
protected FormBuilder newForm(String action, String method) {
62+
return new FormBuilder(action, method);
6363
}
6464

6565
public static class FormBuilder {
6666
StringBuilder content = new StringBuilder();
6767
String action;
68-
String id = null;
69-
String name = null;
70-
private final boolean doPost;
68+
String id;
69+
String name;
70+
String method;
71+
private String enctype;
7172

7273
public FormBuilder(String action) {
73-
this(action, false);
74+
this.action = action;
7475
}
7576

76-
public FormBuilder(String action, boolean doPost) {
77-
this.action = action;
78-
this.doPost = doPost;
77+
public FormBuilder(String action, String method) {
78+
this(action);
79+
method(method);
80+
}
81+
82+
public String getAction() {
83+
return this.action;
84+
}
85+
86+
public FormBuilder method(String method) {
87+
this.method = method;
88+
return this;
89+
}
90+
91+
public String getMethod() {
92+
return this.method;
93+
}
94+
95+
public FormBuilder enctype(String enctype) {
96+
this.enctype = enctype;
97+
return this;
98+
}
99+
100+
public String getEnctype() {
101+
return this.enctype;
79102
}
80103

81104
public FormBuilder id(String id) {
@@ -108,6 +131,11 @@ private void appendSimpleInput(String type, String name, String value, String...
108131
content.append(" " + toAdd);
109132
content.append("/>");
110133
}
134+
135+
public FormBuilder addFileInput(String name, String value) {
136+
appendSimpleInput("file", name, value);
137+
return this;
138+
}
111139

112140
public FormBuilder addTextArea(String name, String value) {
113141
content.append("<textarea");
@@ -174,8 +202,8 @@ public String toString() {
174202
appendIfSet(form, "action", action);
175203
appendIfSet(form, "id", id);
176204
appendIfSet(form, "name", name);
177-
if(doPost)
178-
form.append("method='post' ");
205+
appendIfSet(form, "enctype", enctype);
206+
appendIfSet(form, "method", method);
179207
form.append(">");
180208
form.append(content.toString());
181209
form.append("</form>");

src/test/java/com/gistlabs/mechanize/form/FormTest.java

+39-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@
1010
import static org.junit.Assert.assertEquals;
1111
import static org.junit.Assert.assertFalse;
1212
import static org.junit.Assert.assertTrue;
13+
14+
import java.io.File;
15+
1316
import com.gistlabs.mechanize.MechanizeTestCase;
1417
import com.gistlabs.mechanize.Page;
1518
import com.gistlabs.mechanize.form.Form;
@@ -42,7 +45,7 @@ public void testEmptyFormWithGetMethod() {
4245
@Test
4346
public void testEmptyFormWithPostMethod() {
4447
agent.addPageRequest("http://test.com",
45-
newHtml("Test Page", newForm("form", true).id("form")));
48+
newHtml("Test Page", newForm("form", "post").id("form")));
4649
agent.addPageRequest("POST", "http://test.com/form", newHtml("OK", ""));
4750

4851
Page page = agent.get("http://test.com");
@@ -312,6 +315,41 @@ public void testSettingValueOfSubmitImageFails() {
312315
form.getSubmitImage(byName("submitImage")).setValue("shouldFail");
313316
}
314317

318+
@Test
319+
public void testSimpleFileUpload() throws Exception {
320+
File tmpFile = File.createTempFile("mechanize", "tmp");
321+
322+
agent.addPageRequest("http://test.com",
323+
newHtml("Test Page", newForm("form").method("post").id("form").enctype("multipart/form-data").addFileInput("fileUpload", "")));
324+
agent.addPageRequest("POST", "http://test.com/form", newHtml("OK", ""));
325+
326+
Page page = agent.get("http://test.com");
327+
Form form = page.forms().get(byId("form"));
328+
form.getUpload(byName("fileUpload")).setValue(tmpFile);
329+
Page response = form.submit();
330+
assertEquals("OK", response.getTitle());
331+
}
332+
333+
/**
334+
* TODO: Confirm if this test should indeed pass (it does...)
335+
* See https://github.com/GistLabs/mechanize/pull/23 for request
336+
*
337+
* @throws Exception
338+
*/
339+
@Test
340+
public void testFileUploadWithNoFile() throws Exception {
341+
agent.addPageRequest("http://test.com",
342+
newHtml("Test Page", newForm("form").method("post").id("form").enctype("multipart/form-data").addFileInput("fileUpload", "")));
343+
agent.addPageRequest("POST", "http://test.com/form", newHtml("OK", ""));
344+
345+
Page page = agent.get("http://test.com");
346+
Form form = page.forms().get(byId("form"));
347+
348+
Page response = form.submit();
349+
assertEquals("OK", response.getTitle());
350+
}
351+
352+
315353
//TODO Find a better test
316354
// @Test
317355
// public void testFileUploadingByUsingMegaFileUpload() {

src/test/java/com/gistlabs/mechanize/integration/test/AmazonAddItemToCartAndUseASecondAgentToRemoveTheItemIT.java

-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
import com.gistlabs.mechanize.Page;
1616
import com.gistlabs.mechanize.cookie.Cookie;
1717
import com.gistlabs.mechanize.form.Form;
18-
import com.gistlabs.mechanize.form.SubmitImage;
1918

2019
import org.junit.Test;
2120

0 commit comments

Comments
 (0)