source: trunk/autoquest-test-utils/src/main/java/de/ugoe/cs/autoquest/http/HTTPTestUtils.java @ 1561

Last change on this file since 1561 was 1561, checked in by pharms, 10 years ago
  • update of namespaces for HTTP logfiles
File size: 12.2 KB
Line 
1//   Copyright 2012 Georg-August-Universität Göttingen, Germany
2//
3//   Licensed under the Apache License, Version 2.0 (the "License");
4//   you may not use this file except in compliance with the License.
5//   You may obtain a copy of the License at
6//
7//       http://www.apache.org/licenses/LICENSE-2.0
8//
9//   Unless required by applicable law or agreed to in writing, software
10//   distributed under the License is distributed on an "AS IS" BASIS,
11//   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12//   See the License for the specific language governing permissions and
13//   limitations under the License.
14
15package de.ugoe.cs.autoquest.http;
16
17import static org.junit.Assert.assertEquals;
18import static org.junit.Assert.assertNull;
19
20import java.math.BigInteger;
21
22import de.ugoe.cs.autoquest.plugin.http.logdata.Address;
23import de.ugoe.cs.autoquest.plugin.http.logdata.Content;
24import de.ugoe.cs.autoquest.plugin.http.logdata.Cookie;
25import de.ugoe.cs.autoquest.plugin.http.logdata.Cookies;
26import de.ugoe.cs.autoquest.plugin.http.logdata.Header;
27import de.ugoe.cs.autoquest.plugin.http.logdata.Headers;
28import de.ugoe.cs.autoquest.plugin.http.logdata.HttpExchange;
29import de.ugoe.cs.autoquest.plugin.http.logdata.HttpRequest;
30import de.ugoe.cs.autoquest.plugin.http.logdata.HttpResponse;
31import de.ugoe.cs.autoquest.plugin.http.logdata.Method;
32import de.ugoe.cs.autoquest.plugin.http.logdata.ObjectFactory;
33import de.ugoe.cs.autoquest.plugin.http.logdata.Protocol;
34import de.ugoe.cs.autoquest.plugin.http.logdata.Session;
35import de.ugoe.cs.autoquest.plugin.http.logdata.Status;
36
37/**
38 *
39 */
40public class HTTPTestUtils {
41
42    private static ObjectFactory factory = new ObjectFactory();
43
44    /**
45     *
46     */
47    public static Session createRandomSession(int maxSize) {
48        ObjectFactory factory = new ObjectFactory();
49        Session session = factory.createSession();
50       
51        int count = (int) (maxSize * Math.random());
52       
53        System.out.println("creating a session with " + count + " exchanges");
54        for (int i = 0; i < count; i++) {
55            session.getHttpExchange().add(createRandomExchange());
56        }
57       
58        return session;
59    }
60
61    /**
62     *
63     */
64    public static HttpExchange createRandomExchange() {
65        HttpExchange exchange = factory.createHttpExchange();
66       
67        exchange.setSender(createRandomAddress());
68        exchange.setReceiver(createRandomAddress());
69       
70        HttpRequest request = factory.createHttpRequest();
71        request.setAuthType("AUTH_" + Math.random());
72        request.setQuery("QUERY_" + Math.random());
73        request.setRemoteUser("REMOTE_USER_" + Math.random());
74        request.setRequestedSessionId("REQUESTED_SESSION_ID_" + Math.random());
75        request.setUrl("URL_" + Math.random());
76        request.setContent(createRandomContent());
77        request.setCookies(createRandomCookies());
78        request.setHeaders(createRandomHeaders());
79        request.setMethod(createRandomMethod());
80        request.setProtocol(createRandomProtocol());
81        exchange.setRequest(request);
82       
83        HttpResponse response = factory.createHttpResponse();
84        response.setContent(createRandomContent());
85        response.setHeaders(createRandomHeaders());
86        response.setStatus(BigInteger.valueOf((int) (500 * Math.random())));
87        exchange.setResponse(response);
88       
89        exchange.setStatus(createRandomStatus());
90       
91        return exchange;
92    }
93
94    /**
95     *
96     */
97    public static Address createRandomAddress() {
98        Address address = factory.createAddress();
99       
100        int variant = (int) (7 * Math.random());
101       
102        switch (variant) {
103            case 0:
104                address.setHost("host");
105                address.setIp("123.23.45.5");
106                address.setPort(BigInteger.valueOf(2342));
107                break;
108            case 1:
109                address.setIp("123.23.45.5");
110                address.setPort(BigInteger.valueOf(2342));
111                break;
112            case 2:
113                address.setHost("host");
114                address.setPort(BigInteger.valueOf(2342));
115                break;
116            case 3:
117                address.setHost("host");
118                address.setIp("123.23.45.5");
119                break;
120            case 4:
121                address.setIp("123.23.45.5");
122                break;
123            case 5:
124                address.setHost("host");
125                break;
126            default:
127                address.setPort(BigInteger.valueOf(2342));
128                break;
129        }
130       
131        return address;
132    }
133
134    /**
135     *
136     */
137    public static Cookies createRandomCookies() {
138        Cookies cookies = factory.createCookies();
139       
140        int counter = (int) (5 * Math.random());
141       
142        for (int i = 0; i < counter; i++) {
143            Cookie cookie = factory.createCookie();
144           
145            cookie.setComment("COMMENT_" + Math.random());
146            cookie.setDomain("DOMAIN_" + Math.random());
147            cookie.setIsHttpOnly(Math.random() > 0.5);
148            cookie.setIsSecure(Math.random() > 0.5);
149            cookie.setMaxAge(BigInteger.valueOf((int) (1000 * Math.random())));
150            cookie.setName("NAME_" + Math.random());
151            cookie.setPath("PATH_" + Math.random());
152            cookie.setValue("VALUE_" + Math.random());
153            cookie.setVersion(BigInteger.valueOf((int) (1000 * Math.random())));
154           
155            cookies.getCookie().add(cookie);
156        }
157       
158        return cookies;
159    }
160
161    /**
162     *
163     */
164    public static Method createRandomMethod() {
165        int variant = (int) (8 * Math.random());
166       
167        switch (variant) {
168            case 0:
169                return Method.CONNECT;
170            case 1:
171                return Method.DELETE;
172            case 2:
173                return Method.GET;
174            case 3:
175                return Method.HEAD;
176            case 4:
177                return Method.OPTIONS;
178            case 5:
179                return Method.POST;
180            case 6:
181                return Method.TRACE;
182            default:
183                return Method.PUT;
184        }
185    }
186
187    /**
188     *
189     */
190    public static Protocol createRandomProtocol() {
191        int variant = (int) (3 * Math.random());
192       
193        switch (variant) {
194            case 0:
195                return Protocol.HTTP_1_0;
196            default:
197                return Protocol.HTTP_1_1;
198        }
199    }
200
201    /**
202     *
203     */
204    public static Content createRandomContent() {
205        Content content = factory.createContent();
206       
207        content.setEncoding("ENVODING_" + Math.random());
208        content.setType("TYPE_" + Math.random());
209       
210        int length = (int) (1000 * Math.random());
211
212        StringBuffer data = new StringBuffer();
213        for (int i = 0; i < length; i++) {
214            data.append((char) (33 + (1000 * Math.random())));
215        }
216       
217        content.setLength(data.length());
218        content.setData(data.toString());
219       
220        return content;
221    }
222
223    /**
224     *
225     */
226    public static Headers createRandomHeaders() {
227        Headers headers = factory.createHeaders();
228       
229        int counter = (int) (5 * Math.random());
230       
231        for (int i = 0; i < counter; i++) {
232            Header header = factory.createHeader();
233            header.setKey("KEY_" + Math.random());
234            header.setValue("VALUE_" + Math.random());
235            headers.getHeader().add(header);
236        }
237       
238        return headers;
239    }
240
241    /**
242     *
243     */
244    public static Status createRandomStatus() {
245        int variant = (int) (3 * Math.random());
246       
247        switch (variant) {
248            case 0:
249                return Status.SUCCESS;
250            case 1:
251                return Status.FAILURE;
252            default:
253                return Status.TIMEOUT;
254        }
255    }
256   
257    /**
258     *
259     */
260    public static void assertExchangeEquals(HttpExchange exchange1, HttpExchange exchange2) {
261        assertAddressEquals(exchange1.getSender(), exchange2.getSender());
262        assertAddressEquals(exchange1.getReceiver(), exchange2.getReceiver());
263        assertRequestEquals(exchange1.getRequest(), exchange2.getRequest());
264        assertResponseEquals(exchange1.getResponse(), exchange2.getResponse());
265       
266        assertEquals(exchange1.getStatus(), exchange2.getStatus());
267    }
268
269    /**
270     *
271     */
272    public static void assertAddressEquals(Address address1, Address address2) {
273        if (address1 == null) {
274            assertNull(address2);
275        }
276        else {
277            assertEquals(address1.getHost(), address2.getHost());
278            assertEquals(address1.getIp(), address2.getIp());
279            assertEquals(address1.getPort(), address2.getPort());
280        }
281    }
282
283    /**
284     *
285     */
286    public static void assertRequestEquals(HttpRequest request1, HttpRequest request2) {
287        assertEquals(request1.getAuthType(), request2.getAuthType());
288        assertEquals(request1.getContent().getData(), request2.getContent().getData());
289        assertEquals(request1.getContent().getEncoding(), request2.getContent().getEncoding());
290        assertEquals(request1.getContent().getLength(), request2.getContent().getLength());
291        assertEquals(request1.getContent().getType(), request2.getContent().getType());
292        assertEquals(request1.getMethod(), request2.getMethod());
293        assertEquals(request1.getProtocol(), request2.getProtocol());
294        assertEquals(request1.getQuery(), request2.getQuery());
295        assertEquals(request1.getRemoteUser(), request2.getRemoteUser());
296        assertEquals(request1.getRequestedSessionId(), request2.getRequestedSessionId());
297        assertEquals(request1.getUrl(), request2.getUrl());
298       
299        assertCookiesEqual(request1.getCookies(), request2.getCookies());
300        assertHeadersEqual(request1.getHeaders(), request2.getHeaders());
301    }
302
303    /**
304     *
305     */
306    public static void assertCookiesEqual(Cookies cookies1, Cookies cookies2) {
307        if (cookies1 == null) {
308            assertNull(cookies2);
309        }
310        else {
311            assertEquals(cookies1.getCookie().size(), cookies2.getCookie().size());
312           
313            for (int i = 0; i < cookies1.getCookie().size(); i++) {
314                Cookie cookie1 = cookies1.getCookie().get(i);
315                Cookie cookie2 = cookies2.getCookie().get(i);
316                assertEquals(cookie1.getComment(), cookie2.getComment());
317                assertEquals(cookie1.getDomain(), cookie2.getDomain());
318                assertEquals(cookie1.getMaxAge(), cookie2.getMaxAge());
319                assertEquals(cookie1.getName(), cookie2.getName());
320                assertEquals(cookie1.getPath(), cookie2.getPath());
321                assertEquals(cookie1.getValue(), cookie2.getValue());
322                assertEquals(cookie1.getVersion(), cookie2.getVersion());
323            }
324        }
325    }
326
327    /**
328     *
329     */
330    public static void assertHeadersEqual(Headers headers1, Headers headers2) {
331        if (headers1 == null) {
332            assertNull(headers2);
333        }
334        else {
335            assertEquals(headers1.getHeader().size(), headers2.getHeader().size());
336           
337            for (int i = 0; i < headers1.getHeader().size(); i++) {
338                Header header1 = headers1.getHeader().get(i);
339                Header header2 = headers2.getHeader().get(i);
340                assertEquals(header1.getKey(), header2.getKey());
341                assertEquals(header1.getValue(), header2.getValue());
342            }
343        }
344    }
345
346    /**
347     *
348     */
349    public static void assertResponseEquals(HttpResponse response1, HttpResponse response2) {
350        assertEquals(response1.getStatus(), response2.getStatus());
351        assertEquals(response1.getContent().getData(), response2.getContent().getData());
352        assertEquals(response1.getContent().getEncoding(), response2.getContent().getEncoding());
353        assertEquals(response1.getContent().getLength(), response2.getContent().getLength());
354        assertEquals(response1.getContent().getType(), response2.getContent().getType());
355       
356        assertHeadersEqual(response1.getHeaders(), response2.getHeaders());
357    }
358
359    /**
360     *
361     */
362    private HTTPTestUtils() {
363        // prevent instantiation
364    }
365}
Note: See TracBrowser for help on using the repository browser.