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

Last change on this file since 2017 was 2017, checked in by pharms, 9 years ago

added ordering id stuff

File size: 12.5 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        try {
71            Thread.sleep(2);
72        }
73        catch (InterruptedException e) {
74            // ignore
75        }
76
77        HttpRequest request = factory.createHttpRequest();
78        request.setOrderingId(System.currentTimeMillis());
79        request.setAuthType("AUTH_" + Math.random());
80        request.setQuery("QUERY_" + Math.random());
81        request.setRemoteUser("REMOTE_USER_" + Math.random());
82        request.setRequestedSessionId("REQUESTED_SESSION_ID_" + Math.random());
83        request.setUrl("URL_" + Math.random());
84        request.setContent(createRandomContent());
85        request.setCookies(createRandomCookies());
86        request.setHeaders(createRandomHeaders());
87        request.setMethod(createRandomMethod());
88        request.setProtocol(createRandomProtocol());
89        exchange.setRequest(request);
90       
91        try {
92            Thread.sleep(2);
93        }
94        catch (InterruptedException e) {
95            // ignore
96        }
97       
98        HttpResponse response = factory.createHttpResponse();
99        response.setOrderingId(System.currentTimeMillis());
100        response.setContent(createRandomContent());
101        response.setHeaders(createRandomHeaders());
102        response.setStatus(BigInteger.valueOf((int) (500 * Math.random())));
103        exchange.setResponse(response);
104       
105        exchange.setStatus(createRandomStatus());
106       
107        return exchange;
108    }
109
110    /**
111     *
112     */
113    public static Address createRandomAddress() {
114        Address address = factory.createAddress();
115       
116        int variant = (int) (7 * Math.random());
117       
118        switch (variant) {
119            case 0:
120                address.setHost("host");
121                address.setIp("123.23.45.5");
122                address.setPort(BigInteger.valueOf(2342));
123                break;
124            case 1:
125                address.setIp("123.23.45.5");
126                address.setPort(BigInteger.valueOf(2342));
127                break;
128            case 2:
129                address.setHost("host");
130                address.setPort(BigInteger.valueOf(2342));
131                break;
132            case 3:
133                address.setHost("host");
134                address.setIp("123.23.45.5");
135                break;
136            case 4:
137                address.setIp("123.23.45.5");
138                break;
139            case 5:
140                address.setHost("host");
141                break;
142            default:
143                address.setPort(BigInteger.valueOf(2342));
144                break;
145        }
146       
147        return address;
148    }
149
150    /**
151     *
152     */
153    public static Cookies createRandomCookies() {
154        Cookies cookies = factory.createCookies();
155       
156        int counter = (int) (5 * Math.random());
157       
158        for (int i = 0; i < counter; i++) {
159            Cookie cookie = factory.createCookie();
160           
161            cookie.setComment("COMMENT_" + Math.random());
162            cookie.setDomain("DOMAIN_" + Math.random());
163            cookie.setIsHttpOnly(Math.random() > 0.5);
164            cookie.setIsSecure(Math.random() > 0.5);
165            cookie.setMaxAge(BigInteger.valueOf((int) (1000 * Math.random())));
166            cookie.setName("NAME_" + Math.random());
167            cookie.setPath("PATH_" + Math.random());
168            cookie.setValue("VALUE_" + Math.random());
169            cookie.setVersion(BigInteger.valueOf((int) (1000 * Math.random())));
170           
171            cookies.getCookie().add(cookie);
172        }
173       
174        return cookies;
175    }
176
177    /**
178     *
179     */
180    public static Method createRandomMethod() {
181        int variant = (int) (8 * Math.random());
182       
183        switch (variant) {
184            case 0:
185                return Method.CONNECT;
186            case 1:
187                return Method.DELETE;
188            case 2:
189                return Method.GET;
190            case 3:
191                return Method.HEAD;
192            case 4:
193                return Method.OPTIONS;
194            case 5:
195                return Method.POST;
196            case 6:
197                return Method.TRACE;
198            default:
199                return Method.PUT;
200        }
201    }
202
203    /**
204     *
205     */
206    public static Protocol createRandomProtocol() {
207        int variant = (int) (3 * Math.random());
208       
209        switch (variant) {
210            case 0:
211                return Protocol.HTTP_1_0;
212            default:
213                return Protocol.HTTP_1_1;
214        }
215    }
216
217    /**
218     *
219     */
220    public static Content createRandomContent() {
221        Content content = factory.createContent();
222       
223        content.setEncoding("ENVODING_" + Math.random());
224        content.setType("TYPE_" + Math.random());
225       
226        int length = (int) (1000 * Math.random());
227
228        StringBuffer data = new StringBuffer();
229        for (int i = 0; i < length; i++) {
230            data.append((char) (33 + (1000 * Math.random())));
231        }
232       
233        content.setLength(data.length());
234        content.setData(data.toString());
235       
236        return content;
237    }
238
239    /**
240     *
241     */
242    public static Headers createRandomHeaders() {
243        Headers headers = factory.createHeaders();
244       
245        int counter = (int) (5 * Math.random());
246       
247        for (int i = 0; i < counter; i++) {
248            Header header = factory.createHeader();
249            header.setKey("KEY_" + Math.random());
250            header.setValue("VALUE_" + Math.random());
251            headers.getHeader().add(header);
252        }
253       
254        return headers;
255    }
256
257    /**
258     *
259     */
260    public static Status createRandomStatus() {
261        int variant = (int) (3 * Math.random());
262       
263        switch (variant) {
264            case 0:
265                return Status.SUCCESS;
266            case 1:
267                return Status.FAILURE;
268            default:
269                return Status.TIMEOUT;
270        }
271    }
272   
273    /**
274     *
275     */
276    public static void assertExchangeEquals(HttpExchange exchange1, HttpExchange exchange2) {
277        assertAddressEquals(exchange1.getSender(), exchange2.getSender());
278        assertAddressEquals(exchange1.getReceiver(), exchange2.getReceiver());
279        assertRequestEquals(exchange1.getRequest(), exchange2.getRequest());
280        assertResponseEquals(exchange1.getResponse(), exchange2.getResponse());
281       
282        assertEquals(exchange1.getStatus(), exchange2.getStatus());
283    }
284
285    /**
286     *
287     */
288    public static void assertAddressEquals(Address address1, Address address2) {
289        if (address1 == null) {
290            assertNull(address2);
291        }
292        else {
293            assertEquals(address1.getHost(), address2.getHost());
294            assertEquals(address1.getIp(), address2.getIp());
295            assertEquals(address1.getPort(), address2.getPort());
296        }
297    }
298
299    /**
300     *
301     */
302    public static void assertRequestEquals(HttpRequest request1, HttpRequest request2) {
303        assertEquals(request1.getAuthType(), request2.getAuthType());
304        assertEquals(request1.getContent().getData(), request2.getContent().getData());
305        assertEquals(request1.getContent().getEncoding(), request2.getContent().getEncoding());
306        assertEquals(request1.getContent().getLength(), request2.getContent().getLength());
307        assertEquals(request1.getContent().getType(), request2.getContent().getType());
308        assertEquals(request1.getMethod(), request2.getMethod());
309        assertEquals(request1.getProtocol(), request2.getProtocol());
310        assertEquals(request1.getQuery(), request2.getQuery());
311        assertEquals(request1.getRemoteUser(), request2.getRemoteUser());
312        assertEquals(request1.getRequestedSessionId(), request2.getRequestedSessionId());
313        assertEquals(request1.getUrl(), request2.getUrl());
314       
315        assertCookiesEqual(request1.getCookies(), request2.getCookies());
316        assertHeadersEqual(request1.getHeaders(), request2.getHeaders());
317    }
318
319    /**
320     *
321     */
322    public static void assertCookiesEqual(Cookies cookies1, Cookies cookies2) {
323        if (cookies1 == null) {
324            assertNull(cookies2);
325        }
326        else {
327            assertEquals(cookies1.getCookie().size(), cookies2.getCookie().size());
328           
329            for (int i = 0; i < cookies1.getCookie().size(); i++) {
330                Cookie cookie1 = cookies1.getCookie().get(i);
331                Cookie cookie2 = cookies2.getCookie().get(i);
332                assertEquals(cookie1.getComment(), cookie2.getComment());
333                assertEquals(cookie1.getDomain(), cookie2.getDomain());
334                assertEquals(cookie1.getMaxAge(), cookie2.getMaxAge());
335                assertEquals(cookie1.getName(), cookie2.getName());
336                assertEquals(cookie1.getPath(), cookie2.getPath());
337                assertEquals(cookie1.getValue(), cookie2.getValue());
338                assertEquals(cookie1.getVersion(), cookie2.getVersion());
339            }
340        }
341    }
342
343    /**
344     *
345     */
346    public static void assertHeadersEqual(Headers headers1, Headers headers2) {
347        if (headers1 == null) {
348            assertNull(headers2);
349        }
350        else {
351            assertEquals(headers1.getHeader().size(), headers2.getHeader().size());
352           
353            for (int i = 0; i < headers1.getHeader().size(); i++) {
354                Header header1 = headers1.getHeader().get(i);
355                Header header2 = headers2.getHeader().get(i);
356                assertEquals(header1.getKey(), header2.getKey());
357                assertEquals(header1.getValue(), header2.getValue());
358            }
359        }
360    }
361
362    /**
363     *
364     */
365    public static void assertResponseEquals(HttpResponse response1, HttpResponse response2) {
366        assertEquals(response1.getStatus(), response2.getStatus());
367        assertEquals(response1.getContent().getData(), response2.getContent().getData());
368        assertEquals(response1.getContent().getEncoding(), response2.getContent().getEncoding());
369        assertEquals(response1.getContent().getLength(), response2.getContent().getLength());
370        assertEquals(response1.getContent().getType(), response2.getContent().getType());
371       
372        assertHeadersEqual(response1.getHeaders(), response2.getHeaders());
373    }
374
375    /**
376     *
377     */
378    private HTTPTestUtils() {
379        // prevent instantiation
380    }
381}
Note: See TracBrowser for help on using the repository browser.