Lines 18-65
Link Here
|
18 |
|
18 |
|
19 |
package org.apache.jmeter.protocol.http.proxy; |
19 |
package org.apache.jmeter.protocol.http.proxy; |
20 |
|
20 |
|
|
|
21 |
import java.io.ByteArrayInputStream; |
22 |
import java.io.IOException; |
23 |
|
21 |
import org.apache.jmeter.config.Arguments; |
24 |
import org.apache.jmeter.config.Arguments; |
22 |
import org.apache.jmeter.junit.JMeterTestCase; |
25 |
import org.apache.jmeter.junit.JMeterTestCase; |
23 |
import org.apache.jmeter.protocol.http.sampler.HTTPSamplerBase; |
26 |
import org.apache.jmeter.protocol.http.sampler.HTTPSamplerBase; |
24 |
import org.apache.jmeter.testelement.property.JMeterProperty; |
27 |
import org.apache.jmeter.protocol.http.util.HTTPArgument; |
25 |
import org.apache.jmeter.testelement.property.PropertyIterator; |
|
|
26 |
|
28 |
|
27 |
public class TestHttpRequestHdr extends JMeterTestCase { |
29 |
public class TestHttpRequestHdr extends JMeterTestCase { |
28 |
public TestHttpRequestHdr(String name) { |
30 |
public TestHttpRequestHdr(String name) { |
29 |
super(name); |
31 |
super(name); |
30 |
} |
32 |
} |
31 |
|
33 |
|
32 |
public void setUp() { |
34 |
public void testRepeatedArguments() throws Exception { |
33 |
} |
35 |
// A HTTP GET request |
|
|
36 |
String TEST_GET_REQ = |
37 |
"GET http://localhost/matrix.html" |
38 |
+ "?update=yes&d=1&d=2&d=&d=&d=&d=&d=&d=1&d=2&d=1&d=&d= " |
39 |
+ "HTTP/1.0\n\n"; |
40 |
HTTPSamplerBase s = getSamplerForRequest(TEST_GET_REQ, "UTF-8"); |
41 |
assertEquals(HTTPSamplerBase.GET, s.getMethod()); |
34 |
|
42 |
|
35 |
public void testRepeatedArguments() throws Exception { |
43 |
// Check arguments |
36 |
String TEST_REQ = |
44 |
Arguments arguments = s.getArguments(); |
37 |
"GET http://localhost/matrix.html" |
45 |
assertEquals(13, arguments.getArgumentCount()); |
38 |
+ "?update=yes&d=1&d=2&d=&d=&d=&d=&d=&d=1&d=2&d=1&d=&d= " |
46 |
checkArgument((HTTPArgument)arguments.getArgument(0), "update", "yes", false); |
39 |
+ "HTTP/1.0\n\n"; |
47 |
checkArgument((HTTPArgument)arguments.getArgument(1), "d", "1", false); |
40 |
HttpRequestHdr req = new HttpRequestHdr(); |
48 |
checkArgument((HTTPArgument)arguments.getArgument(2), "d", "2", false); |
41 |
req.parse(new java.io.ByteArrayInputStream(TEST_REQ.getBytes())); |
49 |
checkArgument((HTTPArgument)arguments.getArgument(3), "d", "", false); |
42 |
HTTPSamplerBase s = req.getSampler(); |
50 |
checkArgument((HTTPArgument)arguments.getArgument(4), "d", "", false); |
43 |
assertEquals(13,s.getArguments().getArguments().size()); |
51 |
checkArgument((HTTPArgument)arguments.getArgument(5), "d", "", false); |
44 |
} |
52 |
checkArgument((HTTPArgument)arguments.getArgument(6), "d", "", false); |
|
|
53 |
checkArgument((HTTPArgument)arguments.getArgument(7), "d", "", false); |
54 |
checkArgument((HTTPArgument)arguments.getArgument(8), "d", "1", false); |
55 |
checkArgument((HTTPArgument)arguments.getArgument(9), "d", "2", false); |
56 |
checkArgument((HTTPArgument)arguments.getArgument(10), "d", "1", false); |
57 |
checkArgument((HTTPArgument)arguments.getArgument(11), "d", "", false); |
58 |
// I see that the value gets trimmed, not sure if that is correct |
59 |
checkArgument((HTTPArgument)arguments.getArgument(12), "d", "", false); |
60 |
|
61 |
// A HTTP POST request |
62 |
String postBody = "update=yes&d=1&d=2&d=&d=&d=&d=&d=&d=1&d=2&d=1&d=&d= "; |
63 |
String TEST_POST_REQ = "POST http://localhost/matrix.html HTTP/1.0\n" |
64 |
+ "Content-type: " |
65 |
+ HTTPSamplerBase.APPLICATION_X_WWW_FORM_URLENCODED |
66 |
+ "; charset=UTF-8\n" |
67 |
+ postBody; |
68 |
s = getSamplerForRequest(TEST_POST_REQ, "UTF-8"); |
69 |
assertEquals(HTTPSamplerBase.POST, s.getMethod()); |
70 |
|
71 |
// Check arguments |
72 |
arguments = s.getArguments(); |
73 |
assertEquals(13, arguments.getArgumentCount()); |
74 |
checkArgument((HTTPArgument)arguments.getArgument(0), "update", "yes", false); |
75 |
checkArgument((HTTPArgument)arguments.getArgument(1), "d", "1", false); |
76 |
checkArgument((HTTPArgument)arguments.getArgument(2), "d", "2", false); |
77 |
checkArgument((HTTPArgument)arguments.getArgument(3), "d", "", false); |
78 |
checkArgument((HTTPArgument)arguments.getArgument(4), "d", "", false); |
79 |
checkArgument((HTTPArgument)arguments.getArgument(5), "d", "", false); |
80 |
checkArgument((HTTPArgument)arguments.getArgument(6), "d", "", false); |
81 |
checkArgument((HTTPArgument)arguments.getArgument(7), "d", "", false); |
82 |
checkArgument((HTTPArgument)arguments.getArgument(8), "d", "1", false); |
83 |
checkArgument((HTTPArgument)arguments.getArgument(9), "d", "2", false); |
84 |
checkArgument((HTTPArgument)arguments.getArgument(10), "d", "1", false); |
85 |
checkArgument((HTTPArgument)arguments.getArgument(11), "d", "", false); |
86 |
checkArgument((HTTPArgument)arguments.getArgument(12), "d", " ", false); |
87 |
|
88 |
// A HTTP POST request, with content-type text/plain |
89 |
TEST_POST_REQ = "POST http://localhost/matrix.html HTTP/1.0\n" |
90 |
+ "Content-type: text/plain; charset=UTF-8\n" |
91 |
+ postBody; |
92 |
s = getSamplerForRequest(TEST_POST_REQ, "UTF-8"); |
93 |
assertEquals(HTTPSamplerBase.POST, s.getMethod()); |
94 |
|
95 |
// Check arguments |
96 |
// We should have one argument, with the value equal to the post body |
97 |
arguments = s.getArguments(); |
98 |
assertEquals(1, arguments.getArgumentCount()); |
99 |
checkArgument((HTTPArgument)arguments.getArgument(0), "", postBody, false); |
100 |
} |
45 |
|
101 |
|
46 |
// TODO: will need changing if arguments can be saved in decoded form |
102 |
// TODO: will need changing if arguments can be saved in decoded form |
47 |
public void testEncodedArguments() throws Exception { |
103 |
public void testEncodedArguments() throws Exception { |
48 |
String TEST_REQ = |
104 |
// A HTTP GET request |
49 |
"GET http://localhost:80/matrix.html" |
105 |
String TEST_GET_REQ = "GET http://localhost:80/matrix.html" |
50 |
+ "?abc" |
106 |
+ "?abc" |
51 |
+ "?SPACE=a+b" |
107 |
+ "?SPACE=a+b" |
52 |
+"&space=a%20b" |
108 |
+ "&space=a%20b" |
53 |
+"&query=What?" |
109 |
+ "&query=What?" |
54 |
+ " HTTP/1.1\n\n"; |
110 |
+ " HTTP/1.1\n\n"; |
55 |
HttpRequestHdr req = new HttpRequestHdr(); |
111 |
HTTPSamplerBase s = getSamplerForRequest(TEST_GET_REQ, "UTF-8"); |
56 |
req.parse(new java.io.ByteArrayInputStream(TEST_REQ.getBytes())); |
112 |
assertEquals(HTTPSamplerBase.GET, s.getMethod()); |
57 |
Arguments arguments = req.getSampler().getArguments(); |
113 |
|
58 |
assertEquals(3,arguments.getArguments().size()); |
114 |
// Check arguments |
59 |
PropertyIterator pi= arguments.iterator(); |
115 |
Arguments arguments = s.getArguments(); |
60 |
JMeterProperty next; |
116 |
assertEquals(3, arguments.getArgumentCount()); |
61 |
next = pi.next(); assertEquals("abc?SPACE=a+b",next.getStringValue()); |
117 |
checkArgument((HTTPArgument)arguments.getArgument(0), "abc?SPACE", "a+b", false); |
62 |
next = pi.next(); assertEquals("space=a%20b",next.getStringValue()); |
118 |
checkArgument((HTTPArgument)arguments.getArgument(1), "space", "a%20b", false); |
63 |
next = pi.next(); assertEquals("query=What?",next.getStringValue()); |
119 |
checkArgument((HTTPArgument)arguments.getArgument(2), "query", "What?", false); |
64 |
} |
120 |
|
|
|
121 |
// A HTTP POST request |
122 |
String postBody = "abc?SPACE=a+b&space=a%20b&query=What?"; |
123 |
String TEST_POST_REQ = "POST http://localhost:80/matrix.html HTTP/1.1\n" |
124 |
+ "Content-type: " |
125 |
+ HTTPSamplerBase.APPLICATION_X_WWW_FORM_URLENCODED |
126 |
+ "; charset=UTF-8\n" |
127 |
+ postBody; |
128 |
s = getSamplerForRequest(TEST_POST_REQ, "UTF-8"); |
129 |
assertEquals(HTTPSamplerBase.POST, s.getMethod()); |
130 |
|
131 |
// Check arguments |
132 |
arguments = s.getArguments(); |
133 |
assertEquals(3, arguments.getArgumentCount()); |
134 |
checkArgument((HTTPArgument)arguments.getArgument(0), "abc?SPACE", "a+b", false); |
135 |
checkArgument((HTTPArgument)arguments.getArgument(1), "space", "a%20b", false); |
136 |
checkArgument((HTTPArgument)arguments.getArgument(2), "query", "What?", false); |
137 |
} |
138 |
|
139 |
private HTTPSamplerBase getSamplerForRequest(String request, String contentEncoding) |
140 |
throws IOException { |
141 |
HttpRequestHdr req = new HttpRequestHdr(); |
142 |
ByteArrayInputStream bis = new ByteArrayInputStream(request.getBytes(contentEncoding)); |
143 |
req.parse(bis); |
144 |
bis.close(); |
145 |
return req.getSampler(); |
146 |
} |
147 |
|
148 |
private void checkArgument( |
149 |
HTTPArgument arg, |
150 |
String expectedName, |
151 |
String expectedValue, |
152 |
boolean expectedEncoded) { |
153 |
assertEquals(expectedName, arg.getName()); |
154 |
assertEquals(expectedValue, arg.getValue()); |
155 |
assertEquals(expectedEncoded, arg.isAlwaysEncoded()); |
156 |
} |
65 |
} |
157 |
} |