From 4ae35b251047950de03f89c18a792a101313fed6 Mon Sep 17 00:00:00 2001 From: thc202 Date: Tue, 18 Sep 2018 09:44:18 +0100 Subject: [PATCH] Add timestamp to request Add timestamp (unix time) to requests sent. --- CHANGELOG.md | 1 + .../org/mozilla/zest/core/v1/ZestRequest.java | 24 ++++++++++++ .../mozilla/zest/impl/CommonsHttpClient.java | 1 + .../zest/test/v1/ZestBasicRunnerUnitTest.java | 2 + .../zest/test/v1/ZestRequestUnitTest.java | 37 +++++++++++++++++++ 5 files changed, 65 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3dabf39b..451c171c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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) diff --git a/src/main/java/org/mozilla/zest/core/v1/ZestRequest.java b/src/main/java/org/mozilla/zest/core/v1/ZestRequest.java index 6426afad..542570f4 100644 --- a/src/main/java/org/mozilla/zest/core/v1/ZestRequest.java +++ b/src/main/java/org/mozilla/zest/core/v1/ZestRequest.java @@ -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 cookies = new ArrayList<>(); @@ -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()); @@ -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. * diff --git a/src/main/java/org/mozilla/zest/impl/CommonsHttpClient.java b/src/main/java/org/mozilla/zest/impl/CommonsHttpClient.java index 2d0cfb9a..ea28806f 100644 --- a/src/main/java/org/mozilla/zest/impl/CommonsHttpClient.java +++ b/src/main/java/org/mozilla/zest/impl/CommonsHttpClient.java @@ -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); diff --git a/src/test/java/org/mozilla/zest/test/v1/ZestBasicRunnerUnitTest.java b/src/test/java/org/mozilla/zest/test/v1/ZestBasicRunnerUnitTest.java index 86acd72a..4047098b 100644 --- a/src/test/java/org/mozilla/zest/test/v1/ZestBasicRunnerUnitTest.java +++ b/src/test/java/org/mozilla/zest/test/v1/ZestBasicRunnerUnitTest.java @@ -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; @@ -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); diff --git a/src/test/java/org/mozilla/zest/test/v1/ZestRequestUnitTest.java b/src/test/java/org/mozilla/zest/test/v1/ZestRequestUnitTest.java index 3cfa45c1..19c730c8 100644 --- a/src/test/java/org/mozilla/zest/test/v1/ZestRequestUnitTest.java +++ b/src/test/java/org/mozilla/zest/test/v1/ZestRequestUnitTest.java @@ -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; @@ -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}}", @@ -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); @@ -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); + } }