-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Extract resource loading #1485
Extract resource loading #1485
Conversation
would like @edwardsph to review as well. one question I have is is |
I've grabbed the PR so I can review but I'm afraid it might not be until Thursday due to other commitments. |
This implementation won't have any performance impact compared to the previous implementation. I just put that Registry in between. The UseCase: |
@edwardsph Keep in mind that you have to register the resource loader before you are able to load feature files from a remote location:
|
Sorry it took a while for me to look at this. I think this code is good and I've been looking at how to use it to load remote features. As a temporary step I added the UrlResourceLoader to the defaults in ResourceLoaderRegistry. Then I went into ScenarioFileReader and changed the following:
I was then able to successfully run:
I got the remote output as hoped. Next I need to try and load another remote feature from within my remote one (as a sibling e.g. http://localhost:8080/remote-resource-sibling.feature). I used a local http server for my testing as I was not sure how to set up a MockServer to host the 2 feature files. I was looking at KarateMockHandlerTest and wondering if I could set up 2 background scenarios to represent the 2 remote features, then run a scenario that calls the first remote which calls the second. Can you point me in the right direction on this please? |
@edwardsph going offline now, but if you need 2 mocks use Java and manage the ports: https://github.com/intuit/karate/blob/develop/karate-core/src/test/java/com/intuit/karate/core/mock/MockTest.java |
I'm not sure how to best contribute to this PR. The only way I could think of was either a replacement PR or ask you to push the original changes so I can add a new PR. For now I thought I would describe additions in this comment but if there is a better way, please let me know.
karate-config.js function fn() {
var port = karate.properties['karate.server.port'] || 8080;
var prefix = karate.properties['karate.ssl'] ? 'https' : 'http';
if (prefix === 'https') {
karate.configure('ssl', true);
}
return {
mockServerUrl: prefix + '://localhost:' + port
}
} call-remote.feature @ignore
Feature:
Scenario:
* def remote = call read(`${mockServerUrl}/remote-helper.feature`)
* match remote.helper() == 'Helped' remote-features.feature (print statements left in for debugging only) Feature:
Scenario: pathMatches('/remote.feature')
* text response =
"""
Feature:
Scenario:
* print "REMOTE"
* def a = { remote: true }
* assert a.remote
"""
Scenario: pathMatches('/remote-call-remote.feature')
* text response =
"""
Feature:
Scenario:
* print "CALL REMOTE"
* def remote = call read(`${mockServerUrl}/remote-helper.feature`)
* match remote.helper() == "Helped"
"""
Scenario: pathMatches('/remote-call-relative.feature')
* text response =
"""
Feature:
Scenario:
* print "CALL REMOTE SIBLING"
* def remote = call read('remote-helper.feature')
* match remote.helper() == "Helped"
"""
Scenario: pathMatches('/remote-helper.feature')
* text response =
"""
Feature:
Scenario:
* print "HELPER"
* def helper = function() { return 'Helped' }
""" RemoteScenarioTest.java package com.intuit.karate.resource;
import com.intuit.karate.Results;
import com.intuit.karate.Runner;
import com.intuit.karate.core.MockServer;
import com.intuit.karate.http.HttpServer;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import static org.junit.jupiter.api.Assertions.assertEquals;
/**
*
* @author edwardsph
*/
class RemoteScenarioTest {
static final Logger logger = LoggerFactory.getLogger(RemoteScenarioTest.class);
static String mockServerUrl = null;
static HttpServer startMockServer() {
MockServer server = MockServer.feature("classpath:com/intuit/karate/resource/remote-features.feature").build();
System.setProperty("karate.server.port", server.getPort() + "");
mockServerUrl = "http://localhost:" + server.getPort();
return server;
}
@BeforeAll
static void beforeAll() {
startMockServer();
}
@Test
void testLocalCallingRemote() {
Results results = Runner.path("classpath:com/intuit/karate/resource/call-remote.feature")
.configDir("classpath:com/intuit/karate/resource")
.tags("~@ignore").parallel(1);
assertEquals(0, results.getFailCount(), results.getErrorMessages());
}
@Test
void testRemote() {
Results results = Runner.path(mockServerUrl + "/remote.feature")
.configDir("classpath:com/intuit/karate/resource")
.tags("~@ignore").parallel(1);
assertEquals(0, results.getFailCount(), results.getErrorMessages());
}
@Test
void testRemoteCallingRemote() {
Results results = Runner.path(mockServerUrl + "/remote-call-remote.feature")
.configDir("classpath:com/intuit/karate/resource")
.tags("~@ignore").parallel(1);
assertEquals(0, results.getFailCount(), results.getErrorMessages());
}
@Test
@Disabled // this will fail since the feature runtime attempts to resolve it based on the temporary file location
void testRemoteCallingSibling() {
Results results = Runner.path(mockServerUrl + "/remote-call-relative.feature")
.configDir("classpath:com/intuit/karate/resource")
.tags("~@ignore").parallel(1);
assertEquals(0, results.getFailCount(), results.getErrorMessages());
}
} The first 3 tests pass, demonstrating:
This is really promising but I think there is more to do:
My ideas here are:
Do you foresee any issues with this or does it look like it would work? |
Just thinking some more about this I remembered @peterquiel suggesting:
That would be a much easier solution and wouldn't need the RemoteResource class - just a pre-processing stage whilst the temp file is created in UrlResourceLoader. I'll try that out. |
closing because this is stale / out of date and may need re-work |
Description
Introduces
ResourceLoader
and aResourceLoaderRegistry
that enables resource loading from other locations or pre process any loaded resources.I added an
UrlResourcesLoader
that loads resources like feature file via http.