Skip to content

Commit

Permalink
Merge pull request #181 from thc202/request-timestamp
Browse files Browse the repository at this point in the history
Add timestamp to request
  • Loading branch information
psiinon authored Sep 19, 2018
2 parents 4d6adb0 + 4ae35b2 commit fd39a0c
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Allow to use JBrowserDriver (JBD) in client launch. [#127](https://github.com/mozilla/zest/pull/127)
- Allow to specify profile when launching Chrome and Firefox. [#152](https://github.com/mozilla/zest/pull/152)
- Allow to obtain a screenshot from the browsers (e.g Chrome, Firefox). [#155](https://github.com/mozilla/zest/pull/155)
- Add timestamp (unix time) to request, to indicate when it was sent. [#181](https://github.com/mozilla/zest/pull/181)

### Changed
- Update PhantomJS driver to 1.4.3. [#120](https://github.com/mozilla/zest/pull/120)
Expand Down
24 changes: 24 additions & 0 deletions src/main/java/org/mozilla/zest/core/v1/ZestRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ public class ZestRequest extends ZestStatement {
/** If true follow redirects, otherwise do not */
private boolean followRedirects = true;

/** The timestamp when the request was sent. */
private long timestamp;

/** Cookie to add to the request */
private List<ZestCookie> cookies = new ArrayList<>();

Expand All @@ -64,6 +67,7 @@ public ZestRequest deepCopy() {
zr.setMethod(this.method);
zr.setHeaders(this.headers);
zr.setFollowRedirects(this.followRedirects);
zr.setTimestamp(this.timestamp);

if (this.getResponse() != null) {
zr.setResponse(this.getResponse().deepCopy());
Expand Down Expand Up @@ -238,6 +242,26 @@ public void setFollowRedirects(boolean followRedirects) {
this.followRedirects = followRedirects;
}

/**
* Gets when the request was sent (unix time).
*
* @return the timestamp when the request was sent.
* @since 0.14.0
*/
public long getTimestamp() {
return timestamp;
}

/**
* Sets when the request was sent (unix time).
*
* @param timestamp the timestamp when the request was sent.
* @since 0.14.0
*/
public void setTimestamp(long timestamp) {
this.timestamp = timestamp;
}

/**
* Move up.
*
Expand Down
1 change: 1 addition & 0 deletions src/main/java/org/mozilla/zest/impl/CommonsHttpClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ public ZestResponse send(ZestRequest req) throws IOException {
String responseHeader = null;
String responseBody = null;
Date start = new Date();
req.setTimestamp(start.getTime());
try {
code = httpclient.executeMethod(method);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import static com.github.tomakehurst.wiremock.client.WireMock.post;
import static com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.byLessThan;

import java.net.URL;
import java.util.HashMap;
Expand Down Expand Up @@ -73,6 +74,7 @@ public void shouldSendAndReceiveHttpMessage() throws Exception {
// Then
request = runner.getLastRequest();
assertThat(request).isNotNull();
assertThat(request.getTimestamp()).isCloseTo(System.currentTimeMillis(), byLessThan(2000L));
assertThat(request.getMethod()).isEqualTo(method);
assertThat(request.getUrl()).isEqualTo(url);
assertThat(request.getHeaders()).isEqualTo(headers);
Expand Down
37 changes: 37 additions & 0 deletions src/test/java/org/mozilla/zest/test/v1/ZestRequestUnitTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,16 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
package org.mozilla.zest.test.v1;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

import java.net.URL;
import java.time.Instant;
import java.util.Date;
import org.junit.Test;
import org.mozilla.zest.core.v1.ZestCookie;
import org.mozilla.zest.core.v1.ZestJSON;
import org.mozilla.zest.core.v1.ZestRequest;
import org.mozilla.zest.core.v1.ZestVariables;

Expand Down Expand Up @@ -69,6 +72,7 @@ public void testDeepCopy() throws Exception {
req.setUrl(new URL("http://www.example.com/app/{{token1}}"));
req.setHeaders("Set-Cookie: test={{token2}}");
req.setData("test={{token3}}&user=12{{token3}}34");
req.setTimestamp(Instant.now().toEpochMilli());
req.addCookie(
new ZestCookie(
"{{token}}.{{token3}}",
Expand All @@ -82,6 +86,7 @@ public void testDeepCopy() throws Exception {

assertTrue(req.getHeaders().equals(req2.getHeaders()));
assertTrue(req.getData().equals(req2.getData()));
assertTrue(req.getTimestamp() == req2.getTimestamp());

ZestCookie cookie = req.getZestCookies().get(0);
ZestCookie cookie2 = req2.getZestCookies().get(0);
Expand All @@ -94,4 +99,36 @@ public void testDeepCopy() throws Exception {
assertTrue(cookie.getExpiryDate().equals(cookie2.getExpiryDate()));
assertTrue(cookie.isSecure() == cookie2.isSecure());
}

@Test
public void shouldSerialiseAndDeserialise() throws Exception {
// Given
ZestRequest request = new ZestRequest();
URL url = new URL("http://example.com/");
request.setUrl(url);
String urlToken = "http://{{host}}/";
request.setUrlToken(urlToken);
String method = "POST";
request.setMethod(method);
String headers = "Header-A: value-a\r\nHeader-B: value-b";
request.setHeaders(headers);
String data = "a=b&c=d";
request.setData(data);
boolean followRedirects = false;
request.setFollowRedirects(followRedirects);
long timestamp = Instant.now().toEpochMilli();
request.setTimestamp(timestamp);
// When
String serialisation = ZestJSON.toString(request);
ZestRequest deserialisedRequest = (ZestRequest) ZestJSON.fromString(serialisation);
// Then
assertThat(deserialisedRequest).isNotSameAs(request);
assertThat(deserialisedRequest.getUrl()).isEqualTo(url);
assertThat(deserialisedRequest.getUrlToken()).isEqualTo(urlToken);
assertThat(deserialisedRequest.getMethod()).isEqualTo(method);
assertThat(deserialisedRequest.getHeaders()).isEqualTo(headers);
assertThat(deserialisedRequest.getData()).isEqualTo(data);
assertThat(deserialisedRequest.isFollowRedirects()).isEqualTo(followRedirects);
assertThat(deserialisedRequest.getTimestamp()).isEqualTo(timestamp);
}
}

0 comments on commit fd39a0c

Please sign in to comment.