File injection for chopper client - flutter

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.

Related

How to setup Mock Server to Test iOS and Android app

I have a mobile application running on iOS and Android, I need to create my functional Automated test cases, however for some scenarios I need specific data , so I think a mock server can help on this. I'm not sure how to start with that as I never setup a mock data server, can someone point to the right direction how to start with that?
It depends on what you mean by mock server.
If you want to make actual network calls you could use services like myjson.com or mocky.io to get the specific json.
If you want to simulate the network calls take a look at MockingJay
https://github.com/kylef/Mockingjay
it will stub the network calls and return the specified json.
I usually have many .json files in my test target and use MockingJay to stub the network and test the response/failures.

How to Update a resource with a large attachment with PUT request in JAX-RS?

I have a large byte file (log file) that I want to upload to server using PUT request. The reason I choose PUT is simply because I can use it to create a new resource or update an existing resource.
My problem is how to handle situation when server or Network disruption happens during PUT request.
That is say I have a huge file, during the transfer of which, Network failure happens. When the network resumes, I dont want to start the entire upload. How would I handle this?
I am using JAX-RS API with RESTeasy implementation.
Some people are using the Content-Range Header to achieve this but many people (like Mark Nottingham) state that this is not legal for requests. Please read the comments to this answer.
Besides there is no support from JAX-RS for this scenario.
If you really have the repeating problem of broken PUT requests I would simply let the client slice the files:
PUT /logs/{id}/1
PUT /logs/{id}/2
PUT /logs/{id}/3
GET /logs/{id} would then return the aggregation of all successful submitted slices.

Test symfony restful api with phpunit and doctrine

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.

How to write a test that uses subdomains in play?

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.

encoding mobile-device versioning for REST Api server

We have restful api over HTTP. Amongst other clients we have also mobile-device clients (e.g. iphone). The issue is that there are several iphone apps in different versions out there (1.0, 2.0). Because they are distributed we don't have control which app-version is calling us.
To identify the app-version on server-side I see following options:
device must append URL parameter (e.g. /foo?iphone-app-version=1.0) : A bit yucky, but good thing is that I can see it always on server-logs (URL is always logged)
we authenticate api-clients with HTTP digest. We could encode the app-version inside the username (e.g. iphone_1_0): Good thing it is logged in server logs, but only works for resources which are exposed as HTTP digest.
device must use custom HTTP-header, e.g. X-IPHONE-APP-VERSION: In my view the cleanest approach, but we don't log HTTP headers in server logs (for log-noise it is switched off). So later analyzation is not possible.
Do you have a preferred approach or any other alternatives?
EDIT: With above versioning I don't mean api-versioning/content-negotiation. It is the version of the mobile-device.
You can use the Accept-Header to allow a client to declare what capabilities it has by identifying what versions of media types it supports. e.g.
mobile app does:
GET /server/foo
Accept: application/vnd.acme.fooappV1+xml
When you introduce new features that are not backward compatible you can tell the new updated clients to send,
GET /server/foo
Accept: application/vnd.acme.fooappV2+xml
Then your server knows the capabilities of the client it is talking to.
You could also get the new clients to do this:
GET /server/foo
Accept: application/vnd.acme.fooappV1+xml, application/vnd.acme.fooappV2+xml
That way you can migrate your server resources over to the new format slowly. If the endpoints deliver application/vnd.acme.fooappV1+xml then the client will revert back to the old way. If the endpoints return application/vnd.acme.fooappV2+xml then the new code can take over.
Using this approach, no URIs need to be changed, so bookmarks and statistics remain valid. Migration to a new format can be done incrementally over time and support for old clients can be gradually phased out.
I decided for custom X-xxx-USER-AGENT one. Main reason to decide against more standard "User-Agent" is that is already "polluted" with http-client library or mobile-device information. A custom X-xxx-USER-AGENT is easier to parse for server and does not intervent http-library which often sets it and could override a custom entry.