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