Coder Social home page Coder Social logo

mirrajabi / okhttp-json-mock Goto Github PK

View Code? Open in Web Editor NEW
245.0 10.0 25.0 124 KB

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

Java 100.00%
android android-library mock mocking retrofit okhttp interceptor json okhttp-json-mock okhttp-mock-json jsonmodel json-response

okhttp-json-mock's Issues

Same path and different HTTP methods

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!

List of strings

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.

HTTP FAILED: java.lang.IllegalStateException: message == null

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 :

  • a BASE_URL = "https://changeme.com"
  • a file hello.json located at assets/api which content is :
    { "hello": "test" }
  • a model class :
    data class Hello(val hello:String)
  • a retrofit service :
    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 ๐Ÿ‘ !

Using library for testing

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!

JSON Array cannot be used as root element

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 library has a dependency on Android

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.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.