Được TopGit lập chỉ mục từ metadata GitHub: orhanobut/mockwebserverplus có 90 sao, viết chủ yếu bằng Java. ✔️ OkHttp mockwebserver with fixtures extension
Tóm tắt dựng từ metadata GitHub của chính dự án — chưa có bài review TopGit. Trang sẽ tự động cập nhật khi bài review đầy đủ được xuất bản.
VÌ SAO CHƯA CÓ REVIEW
TopGit viết bài đầy đủ cho repo có nhiều sao nhất và được yêu cầu nhiều nhất. Trang này là snapshot trong thời gian chờ — xem README gốc ở tab READ ME.
MockWebServer is a great tool for mocking network requests/responses.
In order to add response, you need to set MockResponse body along with all
properties you need
Imagine it with huge json responses. It will obscure the method content and will be barely readable.
Solution
In order to make it more readable, you can use fixtures. Move away your response to the fixtures and just reference them.
MockWebServerPlus is a wrapper which contains MockWebServer with fixtures feature.
Create a fixture yaml file under resources/fixtures
statusCode : 200 // as the name says
delay: 0 // delays the response
headers: // adds to the response
- 'Auth:auth'
- 'key:value'
body: 'common/body_file.json' // can be any path under /fixtures folder
// or inline
body: > // can be any text, json, plain etc. Use > letter for scalar text
{
"array": [
1,
2,
3
],
"boolean": true,
"null": null,
"number": 123,
"object": {
"a": "b",
"c": "d",
"e": "f"
},
"string": "Hello World"
}
Use the file name to reference it. That's it!
@Rule public MockWebServerPlus server = new MockWebServerPlus();
@Test public void readableTest() {
server.enqueue("foo_success");
// execute request
// assert
// verify
}
Use the generated Fixtures.java to reference your fixtures. Read the Generate Fixtures.java part
You may wish to use a custom dispatcher with the mock web server.
Fixtures can be used directly inside a Dispatcher:
new Dispatcher() {
@Override public MockResponse dispatch(RecordedRequest request) throws InterruptedException {
return Fixture.parseFrom("simple).toMockResponse();
}
}
Generate Fixtures.java
You can always use plain text to reference your fixtures.
server.enqueue("foo_success");
but you can also generate Fixtures.java file to have all of them with a task. This will make your code more type-safe.
Put the following task into your build.gradle file and execute it when you add/modify your fixture resources.
Above solution will generate Fixtures.java when you execute it manually. But you might forget to execute it, you can
make it dependent for any task to make it automated. Whenever preBuild is executed, it will also execute this task
preBuild.dependsOn generateFixtures
Install
testCompile 'com.orhanobut:mockwebserverplus:2.0.0'
// This is optional, but in order to be up-to-date with OkHttp changes, you can use the latest version
testCompile 'com.squareup.okhttp3:mockwebserver:3.7.0'
Other proxy methods
MockWebServerPlus.server() // returns MockWebServer instance
MockWebServerPlus.takeRequest() // returns RecordedRequest
MockWebServerPlus.url(String path) // returns url to execute
MockWebServerPlus.setDispatcher(Dispatcher dispatcher) // any custom dispatcher
MockWebServerPlus.enqueue(SocketPolicy socketPolicy) // Useful for network errors, such as DISCONNECT etc
For non-android modules, you may need to add the following tasks to copy your resources into classes dir
task copyTestResources(type: Copy) {
from sourceSets.test.resources
into sourceSets.test.output.classesDir
}
How it works
Also notice that accessing sourceSets should be without android.
Credits
MockWebServer from Square
License
Copyright 2017 Orhan Obut
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
Trang TopGit này là một snapshot — tab "Readme" hiển thị nguyên văn README của repo (đã bỏ link, giữ ảnh). Repo GitHub ở github.com/orhanobut/mockwebserverplus là nguồn chính thức.
orhanobut/mockwebserverplus có bao nhiêu sao?
orhanobut/mockwebserverplus có 90 sao GitHub — tải lại trang để xem số mới nhất, hoặc xem trực tiếp github.com/orhanobut/mockwebserverplus. TopGit phản chiếu số sao của GitHub nhưng không cam kết đến từng phút.
orhanobut/mockwebserverplus có phải mã nguồn mở không?
Có — orhanobut/mockwebserverplus phát hành theo license Apache-2.0, nghĩa là mã nguồn mở để đọc, fork và (tùy license) tái sử dụng. Mã: github.com/orhanobut/mockwebserverplus.
orhanobut/mockwebserverplus còn đang phát triển không?
Commit gần nhất trên orhanobut/mockwebserverplus là 9.5 năm trước (theo timestamp GitHub). Repo có 19 fork — một chỉ báo về mức độ quan tâm của cộng đồng.
orhanobut/mockwebserverplus là gì?
orhanobut/mockwebserverplus (orhanobut/mockwebserverplus) là dự án Java trên GitHub. Theo mô tả gốc: ✔️ OkHttp mockwebserver with fixtures extension
orhanobut/mockwebserverplus so với các dự án Mobile khác thế nào?
orhanobut/mockwebserverplus được TopGit xếp vào nhóm Mobile, với 90 sao GitHub và viết bằng Java. Xem trang chủ đề Mobile trên TopGit để so sánh với các dự án tương tự theo số sao và mức độ hoạt động.
Đọc đầy đủ README ở tab phía trên.
Muốn nghe thêm một ý kiến về mockwebserverplus?
Hỏi một AI đọc được trang này — một cú bấm là có ngay nhận định về mockwebserverplus.