I have a play application that uses subdomains.
Currently I have dns setup on my laptop so I can browse URLS locally like:
subdomain1.myappurl.com:9000/
subdomain2.myappurl.com:9000/
(I have myappurl pointing to 127.0.0.1).
I want to create a test now that uses these URLS (I want to provide the URL)
How can I do this with FakeRequest?
Also, what is FakeRequest, is it a headless browser? I ideally want to create a integration test (but not testing the UI side of things) to make sure data is correctly written to the database when I login/logout etc.
Edit: In light of the OP's comments, there is a way to override the host name in the FakeRequest, by adding it as a header. It appears that request.host is actually set in the Request trait where it's just derived from the headers.
import play.api.http.HeaderNames
FakeRequest(GET, "/something").withHeaders(HeaderNames.HOST -> "sub.domain.com")
The FakeRequest is just passed through the router of theFakeApplication(if using theroutehelper), and what you get is theResult` from the controller function. No headless browser involved here.
What will use a headless browser is the WithBrowser helper.
"go to the right url" in new WithBrowser(webDriver = WebDriverFactory(HTMLUNIT)) {
browser.goTo("google.com")
browser.pageSource must not contain("Bing")
// Do other things ...
}
WithBrowser is kind of overkill for testing whether or not data has been saved.
Related
I want to build an App with support of chopper (or dio, is this is easier), where I can configure fixtures for responses, which are deployed with my app (for testers only).
Example:
The app does a request to api.service.com and would receive a JSON response. Now I want to be sure, that (for testing purpose) the result is always the same at runtime. That's why I add a file (name is independent from the requested uri) into my assets folder. I want to add a checkbox in my dev menu, where I can choose to use the file a the real request.
What could be the best solution for that? Do I need to write a response interceptor for that or is there a better solution, because the interceptor would also do a real request, but I don't need that.
I did redirect to external link in asp.net core in local
public async Task<IActionResult> Fallback(string transactionId, [FromForm] FallbackRequest fallbackRequest)
{
var result = await _opayoPaymentService.FallbackRequest(transactionId, fallbackRequest);
return Redirect($"http://localhost:4200/payment-done/{transactionId}/{result.Status}");
}
http://localhost:4200/ this is local on local environment (Angular project).
Now, the Angular project is on production. The url is http://production-url/
Now I want to redirect the production url.
How can I add both urls? When the project runs in production, then production url should be redirected, when the project runs locally, then localhost url should be redirected.
Your description reminded me the appsetting.json and appsetting.development.json, you can set your url as a variable in the configuration file and use it in your code rather than hard-coding the url into your code. Here's a high vote answer based on this scenario, it also provides some related documents in it.
What I wanna add here is some other ways. First, you can also define the url as a variable, which obtain the value from the server's environment variable, it requires you to set an environment variable before deploy your application.
Another way is storing the url into your database and create a management website to modify the value if it needs to be changed. This method is suitable for those application which already has some management page or is about to design management module for the whole application. The advantage is really flexible and easy to change, such as the web url is changed due to some reason so your app need to change the url corresponding to it. As you know some application really needs to do much configuration when it's running.
I'm trying to pull-of some tests for my RESTful api functions.
For this I did the following:
Installed PHPUnit.
Created a new database for testing.
Created a new enviorment (test) and changed the doctrine config for it.
Created a test.
My problem is this:
When performing a request (somedomain.com/api/somemethod) -> the requested page doesn't know i'm performing a test on it -> so the data it uses is the production/development database and not the 'test' db i have created for the tests.
(the script using test db, the requested page uses normal configurations).
Is there a way to solve it without touching or modifying the API code/behavior?.
Thanks.
Since you said you're requesting somedomain.com I can only suspect you're firing requests over HTTP.
Symfony is made to be easily testable and you can perform functional test without ever making a real HTTP request. Instead, it will make a request object and tell it's kernel to handle it as if it were coming from a real client.
There is a chapter in symfony book on this: Functional tests
If you use method described there (using Symfony BrowserKit client and paths instead of complete urls), Symfony will have it's kernel booted in test environment and will handle request like that.
If, however, for any reason you are unable/don't want to do it that way, and want to fire real HTTP requests, I suggest you to make a file in web directory called app_test.php. In that file you should boot the kernel in test environment and make sure your tests are actually hitting that file (instead of app.php or app_dev.php). However, have in mind that this file will be publicly available and as so, it will cause a security hole so make sure to guard it somehow (check app_dev.php for hints). As an idea, you could require specific key to be provided in request header to allow it to pass on. Or if it will be tested from a single machine, you could also guard it by IP, or whatever works for your case.
I am often working with websites to which I have no code access but for which I need to test javascripts, ajax calls and other resources in locally modified versions.
As an example I needed to test a new creative code originally coming from a DoubleClick server. Instead of working directly on DoubleClick and loose a lot of time waiting for my changes to take effect I needed to manipulate a local copy of that javascript resource.
So I changed my /etc/hosts to point the DoubleClick server to localhost where I run a local web server. This way I was able to test a local script instead of the original.
Of course this redirected all resources and not only the one i was interested in what often results in either weird behaviour as resources become unavailable or a lot of effort on my part to make them available.
Is there a way to replace a specific URL by redirecting it to a local file/URL and let all the others from the same origin be untampered?
I have an issue that hopefully someone could advise me on.
I am creating an enterprise iPhone application that will capture information and write it to a local server.
Depending on which of our sites the user is at will depend on which server the information is written to.
I have created a webservice which I can consume in monotouch and pass it information which it will then write to a SQL database on the local server. The problem I face is how do I go about doing this for all of our locations? If i put a web service at each location then each location will need its own version of the app that write using their webservice (all the servers are on the same network and are not separate)
Can I dynamically consume a webservice by passing the relevant URL?
Should I be taking another approach?
Any advice would be appreciated.
EDIT - I probably should mention we use WCF-style web services that require bindings being generated with SISvcUtil.exe (that is where the WebServiceClient class comes from).
We do something similar where we have a test server and production server and it is simple to change the URL where the web service points to. Obviously this does assume that the web service hosted at all the different locations are the same...
When creating the Client object which consumes the web service you need to specify bindings and an endpointaddress, you can simply change the endpointaddress string to point at the appropriate server. The code below should give you an idea of how to do this...
BasicHttpBinding binding = new BasicHttpBinding();
binding.OpenTimeout = new TimeSpan(0,1,0);
binding.CloseTimeout = new TimeSpan(0,1,0);
binding.SendTimeout = new TimeSpan(0,1,0);
//snip - any other bindings you need to specify...
string fullDomain;
string domain;
if (local)
domain = "local.server.com";
else
domain = "production.server.com";
fullDomain = string.Format("https://{0}/WebService/Service.svc", domain);
EndpointAddress endpointAddress = new EndpointAddress(fullDomain);
WebServiceClient client = new WebServiceClient(binding, endpointAddress);
Yes. There should be either a constructor or a Url property you can set which enables you to pass the URL to your different service endpoints at runtime.
(I presume here that you are talking about multiple instances of the same web service hosted at different URLs)