{"id":17211488,"url":"https://github.com/sloy/okresponsefaker","last_synced_at":"2025-07-13T21:07:10.425Z","repository":{"id":139541960,"uuid":"42719483","full_name":"Sloy/okresponsefaker","owner":"Sloy","description":"OkHttp response faker. Provide \"fire and forget\" fake responses to OkHttp through interceptors.","archived":false,"fork":false,"pushed_at":"2016-12-13T09:34:46.000Z","size":58,"stargazers_count":32,"open_issues_count":0,"forks_count":2,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-06-07T03:37:00.974Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"Java","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Sloy.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2015-09-18T12:03:45.000Z","updated_at":"2023-05-17T09:25:34.000Z","dependencies_parsed_at":"2024-06-28T13:32:22.903Z","dependency_job_id":null,"html_url":"https://github.com/Sloy/okresponsefaker","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/Sloy/okresponsefaker","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Sloy%2Fokresponsefaker","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Sloy%2Fokresponsefaker/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Sloy%2Fokresponsefaker/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Sloy%2Fokresponsefaker/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Sloy","download_url":"https://codeload.github.com/Sloy/okresponsefaker/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Sloy%2Fokresponsefaker/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":265205686,"owners_count":23727511,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-10-15T02:57:26.078Z","updated_at":"2025-07-13T21:07:10.415Z","avatar_url":"https://github.com/Sloy.png","language":"Java","funding_links":[],"categories":[],"sub_categories":[],"readme":"# OkResponseFaker\nOkHttp response faker. Provide \"fire and forget\" or persistent fake responses to OkHttp3 through interceptors.\n\n# Description\nOkResponseFaker uses a special OkHttp Interceptor that allows you to provide a custom response for the next or more requests executed, allowing you to easily debug feedback to special cases in your application.\nYou can fake response status and body using the *FakeResponse* interface. Use simple static data, or build a complex response using JSON objects. Your choice.\n\n# Gradle\nJust import the library from jcenter:\n```groovy\ndependencies {\n    compile 'com.sloydev:okresponsefaker:2.0.0'\n    compile 'com.squareup.okhttp3:okhttp:3.4.1' // requires OkHttp 3\n}\n```\n\nYou can add the optional JSON module if you want to build `JsonFakeResponse` using [JsonAdapters](https://github.com/Sloy/JsonAdapters):\n```groovy\ndependencies {\n    compile 'com.sloydev:okresponsefaker:2.0.0'\n    compile 'com.sloydev:okresponsefaker-json:2.0.0'\n    compile 'com.squareup.okhttp3:okhttp:3.4.1' // requires OkHttp 3\n    compile 'com.sloydev:jsonadapters-core:0.1.0' // included in okresponsefaker-json, but whatever\n    // ...\n}\n```\n\n# Setup\nYou just need to add a **FakeResponseInterceptor** to the end of interceptors chain when creating your OkHttp client.\nThere are two easy of using OkResponseFaker: singleton or custom instance. Check the diferences in the usage section.\n```java\npublic OkHttpClient createClient(){\n  OkHttpClient client = new OkHttpClient();\n  client.interceptors().add(...);\n  client.interceptors().add(ResponseFaker.interceptor()); // singleton way\n  // or\n  client.interceptors().add(fakeResponseInterceptor); // instance way\n  return client;\n}\n```\n\n# Usage\nThe simplest way of using the library is through ResponseFaker class and its static methods. It contains a singleton instance of FakeResponseInteractor and wraps its method calls so you always use the same interactor everywhere. \n\nBut you can also create your own instance of FakeResponseInteractor and call its methods directly. This is useful if you have a dependency injection setup and you want to manage your objects lifecycle yourself. All the examples below apply to both ways, since they use the same methods.\n\n### Fake a response\nProvide any instance of FakeResponse. You can use your own or a built-in one.\n```java\npublic void fakeMyResponse(){\n  ResponseFaker.setNextFakeResponse(new FakeResponse(){\n    @Override\n    public String body() {\n        return \"this is a fake response body\";\n    }\n    @Override\n    public int httpCode() {\n        return 404;\n    }\n    @Override\n    public String mediaType() {\n        return \"text/plain\";\n    }\n  );\n}\n```\n\nRight now the only built-in response type is **EmptyBodyFakeResponse**\n```java\npublic void fakeNotFoundResponse(){\n  ResponseFaker.setNextFakeResponse(new EmptyBodyFakeResponse(404));\n}\n```\n\n### Trigger more than once\nBy defaut fake responses are triggered in a \"fire and forget\" manner, meaning that they will be used once only. To avoid this and keep using the fake response until you manually cancel them use the method **setTriggerOnce(boolean)**\n```java\nResponseFaker.setTriggerOnce(false);\n```\nThis flag doesn't change with a new fake responses, you **must** set it to *true* to revert back to \"fire and forget\" mode, or stop using the fake response with **clearNextFakeResponse()**:\n```java\nResponseFaker.clearNextFakeResponse();\n```\n\n### Json Responses\nIf you use the optional Json module you can very easily fake the contents of a response using objects:\n```java\npublic void fakeEmailInUseResponse(){\n  ResponseFaker.setNextFakeResponse(new JsonFakeResponse(myJsonAdapter,\n                new EmailInUseError(),\n                HTTPCODE_INVALID_REQUEST);\n}\n```\n\nThis is super useful if you already have classes representing your server's errors.\n\n# Best served with\nBy its own this library isn't that great. But you can really take advantage of its power when using it with some debugging tools like [Stetho's dumpapp](http://facebook.github.io/stetho/) or a Debug Drawer with debug actions as seen in [Jake Wharton's u2020](https://github.com/JakeWharton/u2020/).\n\n# Demo\nThere isnt't a demo project for this. I think the snippets above make it clear enough. If you think it isn't and have a simple project demo idea, open an issue or pull request.\n\n# Contributing\nThis little library was made for internal usage, and then published to public usage. It's very small and simple, I've just added what we needed. If you consider that it could have more or nicer features, go ahead an *pull request*! I will gladly accept contributions and new features.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsloy%2Fokresponsefaker","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsloy%2Fokresponsefaker","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsloy%2Fokresponsefaker/lists"}