Skip to content

Commit

Permalink
Merge pull request #3 from intuit/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
joelpramos authored Oct 19, 2020
2 parents fa36967 + 7adf16b commit 3611786
Show file tree
Hide file tree
Showing 62 changed files with 899 additions and 310 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ target/
.settings
.classpath
.vscode
.java-version
*.iml
build/
bin/
Expand Down
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -569,7 +569,7 @@ Note that this is currently not supported for [JUnit 5](#junit-5) `@Karate.Test

### Command Line - Gradle

For Gradle you must extend the test task to allow the `karate.options` to be passed to the runtime (otherwise they get consumed by Gradle itself). To do that, add the following:
For Gradle, you must extend the test task to allow the `karate.options` to be passed to the runtime (otherwise they get consumed by Gradle itself). To do that, add the following:

```yml
test {
Expand All @@ -585,7 +585,11 @@ test {
And then the above command in Gradle would look like:

```
./gradlew test -Dtest=CatsRunner
./gradlew test --tests *CatsRunner
```
or
```
./gradlew test -Dtest.single=CatsRunner
```

### Test Suites
Expand Down
4 changes: 2 additions & 2 deletions examples/consumer-driven-contracts/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
<maven.compiler.version>3.6.0</maven.compiler.version>
<spring.boot.version>2.2.0.RELEASE</spring.boot.version>
<karate.version>0.9.6</karate.version>
<spring.boot.version>2.3.4.RELEASE</spring.boot.version>
<karate.version>2.0.0</karate.version>
</properties>

<dependencies>
Expand Down
2 changes: 1 addition & 1 deletion examples/gatling/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
<maven.compiler.version>3.6.0</maven.compiler.version>
<karate.version>0.9.6</karate.version>
<karate.version>2.0.0</karate.version>
<gatling.plugin.version>3.0.2</gatling.plugin.version>
</properties>

Expand Down
2 changes: 1 addition & 1 deletion examples/mobile-test/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
<maven.compiler.version>3.6.0</maven.compiler.version>
<karate.version>0.9.6</karate.version>
<karate.version>2.0.0</karate.version>
</properties>

<dependencies>
Expand Down
4 changes: 2 additions & 2 deletions examples/mock-servlet/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
<maven.compiler.version>3.6.0</maven.compiler.version>
<spring.boot.version>2.2.0.RELEASE</spring.boot.version>
<karate.version>0.9.6</karate.version>
<spring.boot.version>2.3.4.RELEASE</spring.boot.version>
<karate.version>2.0.0</karate.version>
</properties>

<dependencies>
Expand Down
2 changes: 1 addition & 1 deletion examples/robot-test/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
<maven.compiler.version>3.6.0</maven.compiler.version>
<karate.version>0.9.6</karate.version>
<karate.version>2.0.0</karate.version>
</properties>

<dependencies>
Expand Down
2 changes: 1 addition & 1 deletion examples/ui-test/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
<maven.compiler.version>3.6.0</maven.compiler.version>
<karate.version>0.9.6</karate.version>
<karate.version>2.0.0</karate.version>
</properties>

<dependencies>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,37 +26,11 @@
import com.intuit.karate.Config;
import com.intuit.karate.FileUtils;
import com.intuit.karate.core.ScenarioContext;
import org.apache.http.conn.ssl.LenientSslConnectionSocketFactory;

import static com.intuit.karate.http.Cookie.*;

import com.intuit.karate.http.HttpClient;
import com.intuit.karate.http.HttpRequest;
import com.intuit.karate.http.HttpResponse;
import com.intuit.karate.http.HttpUtils;
import com.intuit.karate.http.MultiPartItem;
import com.intuit.karate.http.MultiValuedMap;
import java.io.IOException;

import java.io.InputStream;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.Proxy;
import java.net.ProxySelector;
import java.net.SocketAddress;
import java.net.URI;
import java.nio.charset.Charset;
import java.security.KeyStore;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Map.Entry;
import java.util.Objects;
import javax.net.ssl.SSLContext;

import com.intuit.karate.http.*;
import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.HttpHost;
import org.apache.http.ParseException;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.CookieStore;
Expand All @@ -67,22 +41,28 @@
import org.apache.http.client.methods.RequestBuilder;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.config.SocketConfig;
import org.apache.http.conn.ssl.NoopHostnameVerifier;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.conn.ssl.TrustAllStrategy;
import org.apache.http.conn.ssl.TrustSelfSignedStrategy;
import org.apache.http.conn.ssl.*;
import org.apache.http.cookie.Cookie;
import org.apache.http.impl.client.BasicCookieStore;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.client.LaxRedirectStrategy;
import org.apache.http.impl.client.*;
import org.apache.http.impl.conn.SystemDefaultRoutePlanner;
import org.apache.http.impl.cookie.BasicClientCookie;
import org.apache.http.protocol.BasicHttpContext;
import org.apache.http.ssl.SSLContextBuilder;
import org.apache.http.ssl.SSLContexts;

import javax.net.ssl.SSLContext;
import java.io.IOException;
import java.io.InputStream;
import java.net.*;
import java.nio.charset.Charset;
import java.security.KeyStore;
import java.time.ZonedDateTime;
import java.time.format.DateTimeParseException;
import java.util.*;
import java.util.Map.Entry;

import static com.intuit.karate.http.Cookie.*;

/**
* @author pthomas3
*/
Expand Down Expand Up @@ -269,6 +249,21 @@ protected void buildCookie(com.intuit.karate.http.Cookie c) {
case PATH:
cookie.setPath(entry.getValue());
break;
case EXPIRES: // add expires field for cookie.
try {
cookie.setExpiryDate(Date.from(ZonedDateTime.parse(entry.getValue(), DTFMTR_RFC1123).toInstant()));
}
catch ( DateTimeParseException ex)
{
System.err.println("ex ->" + ex.getLocalizedMessage());
}
break;
case MAX_AGE: // set max age
int maxAge = Integer.parseInt(entry.getValue());
if (maxAge >= 0) { // only for valid maxAge for cookie expiration.
cookie.setExpiryDate(new Date(System.currentTimeMillis() + (maxAge * 1000)));
}
break;
}
}
if (cookie.getDomain() == null) {
Expand Down Expand Up @@ -328,8 +323,10 @@ protected HttpResponse makeHttpRequest(HttpEntity entity, ScenarioContext contex
response.setStatus(httpResponse.getStatusLine().getStatusCode());
for (Cookie c : cookieStore.getCookies()) {
com.intuit.karate.http.Cookie cookie = new com.intuit.karate.http.Cookie(c.getName(), c.getValue());
cookie.put(DOMAIN, c.getDomain());
cookie.put(PATH, c.getPath());
// while preparing the cookie in buildCookie method we used a BasicClientCookie. This conversion ensures we get path correctly.
BasicClientCookie cc = (BasicClientCookie) c;
cookie.put(DOMAIN, cc.getDomain());
cookie.put(PATH, cc.getPath());
if (c.getExpiryDate() != null) {
cookie.put(EXPIRES, c.getExpiryDate().getTime() + "");
}
Expand All @@ -338,7 +335,17 @@ protected HttpResponse makeHttpRequest(HttpEntity entity, ScenarioContext contex
response.addCookie(cookie);
}
cookieStore.clear(); // we rely on the StepDefs for cookie 'persistence'
for (Header header : httpResponse.getAllHeaders()) {
for (Header header : httpResponse.getAllHeaders()) { // rely on setting cookies from set-cookie header, else these will be skipped.
if( header.getName().equalsIgnoreCase("Set-Cookie"))
{
List<HttpCookie> cookieMap = HttpCookie.parse(header.getValue());
cookieMap.forEach( ck -> {
com.intuit.karate.http.Cookie cookie = new com.intuit.karate.http.Cookie(ck.getName(), ck.getValue());
cookie.put(DOMAIN, ck.getDomain());
cookie.put(PATH, null != ck.getPath() ? ck.getPath() : "/"); // lets make sure path is not null.
cookie.put(MAX_AGE, ck.getMaxAge() + "");
});
}
response.addHeader(header.getName(), header.getValue());
}
return response;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package com.intuit.karate.http.apache;

import com.intuit.karate.CallContext;
import com.intuit.karate.Config;
import com.intuit.karate.core.FeatureContext;
import com.intuit.karate.core.ScenarioContext;
import com.intuit.karate.http.Cookie;
import org.apache.http.client.CookieStore;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.lang.reflect.Field;
import java.time.ZonedDateTime;
import java.util.LinkedHashMap;
import java.util.Map;

import static com.intuit.karate.http.Cookie.*;
import static com.intuit.karate.http.HttpClient.construct;
import static org.junit.Assert.assertEquals;

public class ApacheHttpClientTest {

private static final Logger logger = LoggerFactory.getLogger(ApacheHttpClientTest.class);

private ScenarioContext getContext() {
FeatureContext featureContext = FeatureContext.forEnv();
CallContext callContext = new CallContext(null, true);
return new ScenarioContext(featureContext, callContext, null, null);
}

private Config getConfig() {
return new Config();
}

private Map<String, String> getCookieMapWithExpiredDate() {
ZonedDateTime currentDate = ZonedDateTime.now();
Map<String, String> cookieMap = new LinkedHashMap<>();
cookieMap.put(NAME, "testCookie");
cookieMap.put(VALUE, "tck");
cookieMap.put(DOMAIN, ".com");
cookieMap.put(PATH, "/");
cookieMap.put(EXPIRES,currentDate.minusDays(1).format(DTFMTR_RFC1123));
return cookieMap;
}

private Map<String, String> getCookieMapWithNonExpiredDate() {
ZonedDateTime currentDate = ZonedDateTime.now();
Map<String, String> cookieMap = new LinkedHashMap<>();
cookieMap.put(NAME, "testCookie");
cookieMap.put(VALUE, "tck");
cookieMap.put(DOMAIN, ".com");
cookieMap.put(PATH, "/");
cookieMap.put(EXPIRES, currentDate.plusDays(1).format(DTFMTR_RFC1123));
return cookieMap;
}

@Test
public void testExpiredCookieIsRemoved() throws NoSuchFieldException, IllegalAccessException {
com.intuit.karate.http.Cookie c = new Cookie(getCookieMapWithExpiredDate());
ApacheHttpClient httpClient = (ApacheHttpClient) construct(getConfig(), getContext());
httpClient.buildCookie(c);

Field cookieStoreField = httpClient.getClass().getDeclaredField("cookieStore");
cookieStoreField.setAccessible(true);
CookieStore fieldValue = (CookieStore) cookieStoreField.get(httpClient);
assertEquals(0, fieldValue.getCookies().size());
}

@Test
public void testNonExpiredCookieIsPersisted() throws NoSuchFieldException, IllegalAccessException {
com.intuit.karate.http.Cookie c = new Cookie(getCookieMapWithNonExpiredDate());
ApacheHttpClient httpClient = (ApacheHttpClient) construct(getConfig(), getContext());
httpClient.buildCookie(c);

Field cookieStoreField = httpClient.getClass().getDeclaredField("cookieStore");
cookieStoreField.setAccessible(true);
CookieStore fieldValue = (CookieStore) cookieStoreField.get(httpClient);
assertEquals(1, fieldValue.getCookies().size());
}
}


3 changes: 3 additions & 0 deletions karate-apache/src/test/resources/karate-config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
function fn() {
return { someConfig: 'someValue' }
}
2 changes: 2 additions & 0 deletions karate-apache/src/test/resources/karate-http.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
client.class=com.intuit.karate.http.apache.ApacheHttpClient

4 changes: 2 additions & 2 deletions karate-core/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -491,15 +491,15 @@ The recommendation is that you prefer `chrome` for development, and once you hav

type | default port | default executable | description
---- | ------------ | ------------------ | -----------
[`chrome`](https://chromedevtools.github.io/devtools-protocol/) | 9222 | mac: `/Applications/Google Chrome.app/Contents/MacOS/Google Chrome`<br/>win: `C:/Program Files (x86)/Google/Chrome/Application/chrome.exe` | "native" Chrome automation via the [DevTools protocol](https://chromedevtools.github.io/devtools-protocol/)
[`chrome`](https://chromedevtools.github.io/devtools-protocol/) | 9222 | mac: `/Applications/Google Chrome.app/Contents/MacOS/Google Chrome` <br/>win: `C:/Program Files (x86)/Google/Chrome/Application/chrome.exe` | "native" Chrome automation via the [DevTools protocol](https://chromedevtools.github.io/devtools-protocol/)
[`playwright`](https://playwright.dev) | 4444 | `playwright` | see [`playwrightOptions`](#playwrightoptions) and [Playwright](#playwright)
[`msedge`](https://docs.microsoft.com/en-us/microsoft-edge/) | 9222 | mac: `/Applications/Microsoft Edge.app/Contents/MacOS/Microsoft Edge` <br/>win: `C:/Program Files (x86)/Microsoft/Edge/Application/msedge.exe` | the new Chromium based Microsoft Edge, using the [DevTools protocol](https://docs.microsoft.com/en-us/microsoft-edge/devtools-protocol-chromium)
[`chromedriver`](https://sites.google.com/a/chromium.org/chromedriver/home) | 9515 | `chromedriver` | W3C Chrome Driver
[`geckodriver`](https://github.com/mozilla/geckodriver) | 4444 | `geckodriver` | W3C Gecko Driver (Firefox)
[`safaridriver`](https://webkit.org/blog/6900/webdriver-support-in-safari-10/) | 5555 | `safaridriver` | W3C Safari Driver
[`msedgedriver`](https://developer.microsoft.com/en-us/microsoft-edge/tools/webdriver/) | 9515 | `msedgedriver` | W3C Microsoft Edge WebDriver (the new one based on Chromium), also see [`webDriverSession`](#webdriversession)
[`mswebdriver`](https://developer.microsoft.com/en-us/microsoft-edge/tools/webdriver/) | 17556 | `MicrosoftWebDriver` | Microsoft Edge "Legacy" WebDriver
[`iedriver`](https://github.com/SeleniumHQ/selenium/wiki/InternetExplorerDriver) | 5555 | `IEDriverServer` | IE (11 only) Driver
[`msedge`](https://docs.microsoft.com/en-us/microsoft-edge/devtools-protocol/) | 9222 | `MicrosoftEdge` | *very* experimental - using the DevTools protocol
[`winappdriver`](https://github.com/Microsoft/WinAppDriver) | 4727 | `C:/Program Files (x86)/Windows Application Driver/WinAppDriver` | Windows Desktop automation, similar to Appium
[`android`](https://github.com/appium/appium/) | 4723 | `appium` | android automation via [Appium](https://github.com/appium/appium/)
[`ios`](https://github.com/appium/appium/) | 4723 |`appium` | iOS automation via [Appium](https://github.com/appium/appium/)
Expand Down
Loading

0 comments on commit 3611786

Please sign in to comment.