speekha / httpmocker Goto Github PK
View Code? Open in Web Editor NEWHttpMocker is a simple HTTP mocking library written in Kotlin to quickly and easily handle offline modes in your apps
License: Other
HttpMocker is a simple HTTP mocking library written in Kotlin to quickly and easily handle offline modes in your apps
License: Other
I am using the following builder setup to just to record some canned responses in a directory.
MockResponseInterceptor.Builder()
.parseScenariosWith(GsonMapper())
.loadFileWith { context.assets.open(it) }
.setInterceptorStatus(MockResponseInterceptor.Mode.RECORD)
.saveScenariosIn(File(Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_DOWNLOADS).absolutePath))
.saveScenariosIn(File(Environment.getExternalStorageDirectory().path))
.build()
I also tried File(Environment.getExternalStorageDirectory().path) without any luck
I cannot see any json respons saved or any logs indicating a issue.
Am I missing something?
In version 1.6 interceptor is caching all exceptions throwed during creating ResponseDescriptor
and return default response (404, page not found).
try {
provider.loadResponse(request)?.let { response ->
executeMockResponse(response, request, provider)
}
} catch (e: Throwable) {
logger.error("Scenario file could not be loaded", e)
null
}
There is no possibility to mock any communication exception now. It would be nice to have such option back.
Currently, I am looking for a way to mock a specific API request instead of the whole app?
Documentation generation and publication with Dokka has been temporarily removed in order to let KMM target build. Needs to be updated and reactivated.
I have 2 requests hitting the same endpoint that differ in several ways:
The first request has no params (since Retrofit strips out null
param values):
"request": {
"method": "GET",
"headers": {},
"params": {}
}
The second request does have a param:
"request": {
"method": "GET",
"headers": {},
"params": {
"token": "eyJvYmplY3Rfc2VxIjo2NDUyNTd9"
}
}
The problem is that the matcher always matches the first request, since params.all { request.url().queryParameter(it.key) == it.value }
short-circuits and always returns true for a request that has empty params
list.
Adding params.size == request.url().querySize()
check to the RequestDescriptor.match(request: Request)
fixes the issue, but I'm not sure if that's the desired behavior in every case and/or for this library in general.
Given the different parsing libraries available, developers might not want to include Jackson in their apps if they already use GSON or Moshi. The lib should be modular and allow to include the desired parser.
HttpMocker is only available for JVM compatible platforms (JVM or Android). Other platforms, especially mobile ones like iOS, could benefit from a dedicated version for multiplatform applications.
JVM specific code will have to be isolated so a common project can be extracted and iOS specific implementations can be provided for iOS developers.
Requires support for a multiplatform http client first, like Ktor.
Mockito kotlin hasn't shown sign of support for quite a while. Replacing it with Mockk would be preferable.
How to Mock different requests with different jsons
I have a static scenario with "record". When trying to match them with mode.ENABLED, I get an exception :
ERROR fr.speekha.httpmocker.scenario.StaticMockProvider - Scenario file could not be loaded
java.util.regex.PatternSyntaxException: Illegal repetition
{"data":{"username":"XXX","key":"XXX","project_code":"Explorers_Helidon"},"action":"get_information","group":"projects"}
at java.util.regex.Pattern.error(Pattern.java:1969) ~[?:1.8.0_265]
at java.util.regex.Pattern.closure(Pattern.java:3171) ~[?:1.8.0_265]
at java.util.regex.Pattern.sequence(Pattern.java:2148) ~[?:1.8.0_265]
at java.util.regex.Pattern.expr(Pattern.java:2010) ~[?:1.8.0_265]
at java.util.regex.Pattern.compile(Pattern.java:1702) ~[?:1.8.0_265]
at java.util.regex.Pattern.<init>(Pattern.java:1352) ~[?:1.8.0_265]
at java.util.regex.Pattern.compile(Pattern.java:1028) ~[?:1.8.0_265]
at kotlin.text.Regex.<init>(Regex.kt:89) ~[kotlin-stdlib-1.4.10.jar:1.4.10-release-411 (1.4.10)]
at fr.speekha.httpmocker.io.OkHttpExtensionsKt.matchBody(OkHttpExtensions.kt:46) ~[mocker-1.2.0.jar:?]
at fr.speekha.httpmocker.scenario.RequestMatcher.matchBody(RequestMatcher.kt:37) ~[mocker-1.2.0.jar:?]
To Reproduce
Create a static scenario with :
{
"request" : {
"method" : "POST",
"headers" : { },
"params" : { },
"body" : "{\"data\":{\"username\":\"XXX\",\"key\":\"XXX\",\"project_code\":\"Explorers_Helidon\"},\"action\":\"get_information\",\"group\":\"projects\"}"
},
Expected behavior
Of course the body section of a request is meant to be a regex, but a recorded scenario should be matched "out of the box" to minimize work for the developer.
I experimented a little and an easy solution would be to create the regex in OkHttpExtensions.kt:44 :
Regex(bodyPattern, RegexOption.LITERAL)
, but then normal regex would not work anymore :(
Desktop (please complete the following information):
httpmocker version 1.2.0
Kotlin version 1.4
Retrofit version 2.9.0
OkHttp version 4.8.1
HttpMocker support should be extended to Ktor clients.
OkHttp specific parts need to be isolated in a common module. An okHttp specific module should gather the specific code and a new Ktor module should be created to implement its specificities
Ktor support could allow to design a multiplatform build in the future.
When HttpMocker is used for debuging purposes (mocking unavailable HTTP services), the interceptor should not be included in production code. One way to allow to remove Httpmocker code from production builds is to provide a No-op artifact which would preserve compilation but not include any of the HttpMocker code.
Bintray publication is ending. MavenCentral publication needs to be set up.
Based on existing data classes, and possibly on retrofit services, mock responses could be computed automatically :
A URL can have multiple instances of a single query parameter. Storing them as a Map<String, String> is not valid. They should be stored as List<Pair<String, String>> like headers.
Hi, I've encountered very rare situation in tests, when there are 2 requests sent simultaneously . I've digged into DynamicMockProvider
and it looks like it uses LinkedHashMap which is not synchronized (according to https://docs.oracle.com/javase/8/docs/api/java/util/LinkedHashMap.html). I can't reproduce it easily but probably there is a bug in this line
body[request] = result.body.toByteArray()
- if this executes at the same time for 2 requests then one of them could override another due to lack of synchronization in LinkedHashMap
.
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.