1 |
|
---|
2 | package de.ugoe.cs.quest.plugin.php.eventcore;
|
---|
3 |
|
---|
4 | import java.util.ArrayList;
|
---|
5 | import java.util.List;
|
---|
6 |
|
---|
7 | import de.ugoe.cs.quest.IReplayDecorator;
|
---|
8 | import de.ugoe.cs.quest.eventcore.IReplayable;
|
---|
9 |
|
---|
10 | /**
|
---|
11 | * <p>
|
---|
12 | * Contains all information related to a web request, i.e., the path, the POST variables and the GET
|
---|
13 | * variables. The generated replay are for the command line tool {@code curl}. The requests do not
|
---|
14 | * contain correct values for the POST and GET request. Instead, only the parameters that are part
|
---|
15 | * of the requests are added and the values of the parameters are DATA_$PARAMNAME$_DATA, where
|
---|
16 | * $PARAMNAME$ is the upper case string of the parameter name. This allows test data generators to
|
---|
17 | * insert concrete values, as EventBench does not include a test data generator for web software.
|
---|
18 | * </p>
|
---|
19 | *
|
---|
20 | * @author Steffen Herbold
|
---|
21 | * @version 1.0
|
---|
22 | */
|
---|
23 | public class WebRequest implements IReplayable {
|
---|
24 |
|
---|
25 | /**
|
---|
26 | * <p>
|
---|
27 | * Id for object serialization.
|
---|
28 | * </p>
|
---|
29 | */
|
---|
30 | private static final long serialVersionUID = 1L;
|
---|
31 |
|
---|
32 | /**
|
---|
33 | * <p>
|
---|
34 | * POST variables of the web request.
|
---|
35 | * </p>
|
---|
36 | */
|
---|
37 | List<String> postVars;
|
---|
38 |
|
---|
39 | /**
|
---|
40 | * <p>
|
---|
41 | * GET variables of the web request.
|
---|
42 | * </p>
|
---|
43 | */
|
---|
44 | List<String> getVars;
|
---|
45 |
|
---|
46 | /**
|
---|
47 | * <p>
|
---|
48 | * URI of the web request.
|
---|
49 | * </p>
|
---|
50 | */
|
---|
51 | String targetUri;
|
---|
52 |
|
---|
53 | /**
|
---|
54 | * <p>
|
---|
55 | * URL of the server.
|
---|
56 | * </p>
|
---|
57 | */
|
---|
58 | String serverUrl;
|
---|
59 |
|
---|
60 | /**
|
---|
61 | * <p>
|
---|
62 | * Constructor. Creates a new WebRequest.
|
---|
63 | * </p>
|
---|
64 | *
|
---|
65 | * @param uri
|
---|
66 | * URI of the request
|
---|
67 | * @param postVars
|
---|
68 | * POST variables of the request
|
---|
69 | * @param getVars
|
---|
70 | * GET variables of the request
|
---|
71 | */
|
---|
72 | public WebRequest(String url, String uri, List<String> postVars, List<String> getVars) {
|
---|
73 | serverUrl = url;
|
---|
74 | targetUri = uri;
|
---|
75 | this.postVars = new ArrayList<String>(postVars); // defensive copy
|
---|
76 | this.getVars = new ArrayList<String>(getVars);
|
---|
77 | }
|
---|
78 |
|
---|
79 | /*
|
---|
80 | * (non-Javadoc)
|
---|
81 | *
|
---|
82 | * @see de.ugoe.cs.quest.eventcore.IReplayable#getReplay()
|
---|
83 | */
|
---|
84 | @Override
|
---|
85 | public String getReplay() {
|
---|
86 | StringBuilder builder = new StringBuilder();
|
---|
87 | builder.append("curl");
|
---|
88 | if (!postVars.isEmpty()) {
|
---|
89 | boolean isFirstPost = true;
|
---|
90 | for (String postVar : postVars) {
|
---|
91 | if (isFirstPost) {
|
---|
92 | builder.append(" --data \"");
|
---|
93 | isFirstPost = false;
|
---|
94 | }
|
---|
95 | else {
|
---|
96 | builder.append('&');
|
---|
97 | }
|
---|
98 | builder.append(postVar + "=DATA_" + postVar.toUpperCase() + "_DATA");
|
---|
99 | }
|
---|
100 | builder.append('\"');
|
---|
101 | }
|
---|
102 | builder.append(' ');
|
---|
103 | if (serverUrl != null) {
|
---|
104 | builder.append(serverUrl);
|
---|
105 | }
|
---|
106 | builder.append(targetUri);
|
---|
107 | if (!getVars.isEmpty()) {
|
---|
108 | boolean isFirstGet = true;
|
---|
109 | for (String getVar : getVars) {
|
---|
110 | if (isFirstGet) {
|
---|
111 | builder.append('?');
|
---|
112 | isFirstGet = false;
|
---|
113 | }
|
---|
114 | else {
|
---|
115 | builder.append('&');
|
---|
116 | }
|
---|
117 | builder.append(getVar + "=DATA_" + getVar.toUpperCase() + "_DATA");
|
---|
118 | }
|
---|
119 | }
|
---|
120 | return builder.toString();
|
---|
121 | }
|
---|
122 |
|
---|
123 | /**
|
---|
124 | * <p>
|
---|
125 | * Two {@link WebRequest}s are equal, if their {@link #targetUri}, {@link #postVars}, and
|
---|
126 | * {@link #getVars} are equal.
|
---|
127 | * </p>
|
---|
128 | *
|
---|
129 | * @see java.lang.Object#equals(java.lang.Object)
|
---|
130 | */
|
---|
131 | @Override
|
---|
132 | public boolean equals(Object other) {
|
---|
133 | if (this == other) {
|
---|
134 | return true;
|
---|
135 | }
|
---|
136 | if (other instanceof WebRequest) {
|
---|
137 | return targetUri.equals(((WebRequest) other).targetUri) &&
|
---|
138 | postVars.equals(((WebRequest) other).postVars) &&
|
---|
139 | getVars.equals(((WebRequest) other).getVars);
|
---|
140 | }
|
---|
141 | return false;
|
---|
142 | }
|
---|
143 |
|
---|
144 | /*
|
---|
145 | * (non-Javadoc)
|
---|
146 | *
|
---|
147 | * @see java.lang.Object#hashCode()
|
---|
148 | */
|
---|
149 | @Override
|
---|
150 | public int hashCode() {
|
---|
151 | int multiplier = 17;
|
---|
152 | int hash = 42;
|
---|
153 |
|
---|
154 | hash = multiplier * hash + targetUri.hashCode();
|
---|
155 | hash = multiplier * hash + postVars.hashCode();
|
---|
156 | hash = multiplier * hash + getVars.hashCode();
|
---|
157 |
|
---|
158 | return hash;
|
---|
159 | }
|
---|
160 |
|
---|
161 | /*
|
---|
162 | * (non-Javadoc)
|
---|
163 | *
|
---|
164 | * @see de.ugoe.cs.quest.eventcore.IReplayable#getDecorator()
|
---|
165 | */
|
---|
166 | @Override
|
---|
167 | public IReplayDecorator getDecorator() {
|
---|
168 | return null;
|
---|
169 | }
|
---|
170 |
|
---|
171 | }
|
---|