mirrajabi / okhttp-json-mock Goto Github PK
View Code? Open in Web Editor NEWMock your datas for Okhttp and Retrofit in json format in just a few moves
Home Page: http://mirrajabi.nl
License: Apache License 2.0
Mock your datas for Okhttp and Retrofit in json format in just a few moves
Home Page: http://mirrajabi.nl
License: Apache License 2.0
If I have requests with the same path and different HTTP methods. How do I organize the json models in the assets folder to correspond?
For example, how do I get different responses for GET api/v1/posts and POST api/v1/posts?
Thanks!
What about reading an array of something?
I mean, in the example there is a Observable<ArrayList<String>> getUserNumbers
, but it didnt work. It give me the following error "Error opening asset api/v1/users/1/phonenumbers.json" (ive openned your project and tested in it)
How can this be fixed?
In order to make a "workaroud", I need to create a model object that contains this list, but this didnt feel 'right' to me.
thanks.
Hi guys and thank you for this great lib !
I'm currently using it for the first time and I have a small problem. Maybe it comes from a wrong usage of the lib.
Concretely, I have :
{ "hello": "test" }
data class Hello(val hello:String)
interface HelloService { @GET("api/hello") fun getHello(): Single<Hello> }
Here's how I create my retrofit instance including the mock interceptors on OkHttpClient :
val httpClient = OkHttpClient.Builder()
if (BuildConfig.DEBUG)
httpClient.addInterceptor(HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.BODY))
if (inputStreamProvider != null)
httpClient.addInterceptor(OkHttpMockInterceptor(inputStreamProvider, 5))
val retrofit = Retrofit.Builder()
.addConverterFactory(GsonConverterFactory.create())
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.baseUrl(baseUrl)
.client(httpClient.build())
.build()
Finally, here's my inputStreamProvider instanciation :
private val inputStreamProvider = InputStreamProvider {
try {
assets.open(it)
} catch (e: IOException) {
e.printStackTrace()
}
null
}
The error I have when I use my service is that I haven't the response I expect and I have an error :
D/OkHttp: --> GET https://changeme.com/api/hello
--> END GET
D/OkHttp: <-- HTTP FAILED: java.lang.IllegalStateException: message == null
Has anyone an idea of the problem here ? It will be very useful !
Thank you ๐ !
Hi,
I configured the library for requests in a common scenario (normal apk compilation) and works fine.
I have a problem trying to use this library for mock requests for testing purposes. The problem is that I can't access to AssetManager from unit tests to get the *.json files.
I tried to mock it:
for (path in mockPaths) {
when<Any>( context.assets.open(path)) .thenReturn(getClass().classLoader.getResourceAsStream(path)) }
But this doesn't works. Do you know how the library can be configured for testing?
Thank you!
First of all, I would like to thank you for this awesome and very convenient library!
It makes perfect sense in our emerging application since our API is still under development.
I have an endpoint that responds with a JSON array:
[ { ... }, { ... }, ... ]
I would like to reuse this exact same JSON from the API, but currently I have to adjust these responses before I copy them into the assets in order to match your specification:
{ "status": 200, "response": { "items": <actual array from above> } }
Otherwise I get the following IllegalStateException from GSON, since it expects an object as defined in the MockedResponse.java of your library:
Expected BEGIN_OBJECT but was BEGIN_ARRAY at line 1 column 2 path $
I would like to ditch the surrounding JSON object and parse only the array.
Is this possible?
This is not mentioned in the repo description anywhere, but it makes this library unsuitable for non-Android projects and tests, unless you want to use Robolectric.
Hi! Can I mock POST, PUT, DELETE methods?
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.