Skip to content

Commit 8e8cfb9

Browse files
author
Batu
committed
Merge branch 'develop'
2 parents 68b5d3c + 0b91558 commit 8e8cfb9

File tree

5 files changed

+80
-39
lines changed

5 files changed

+80
-39
lines changed

.idea/gradle.xml

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

app/src/main/java/tw/dcard/bubblemock/sample/api/member/MemberMockScenario.kt

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
package tw.dcard.bubblemock.sample.api.member
22

33
import tw.dcard.bubblemock.model.MockScenario
4-
import tw.dcard.bubblemock.model.api
4+
import tw.dcard.bubblemock.model.apiDetail
5+
import tw.dcard.bubblemock.model.apiRegex
56
import tw.dcard.bubblemock.model.scenario
67
import tw.dcard.bubblemock.module.MockBubbleManager
78
import tw.dcard.bubblemock.sample.model.Member
@@ -13,7 +14,7 @@ object MemberMockScenario {
1314
true
1415
}
1516
add {
16-
api("members") {
17+
apiRegex("members") {
1718
response {
1819
getMembers(5)
1920
}
@@ -22,7 +23,7 @@ object MemberMockScenario {
2223
},
2324
scenario(page = "Main Page", name = "Member List - lots data with long delay") {
2425
add {
25-
api("members") {
26+
apiDetail("members") {
2627
response {
2728
getMembers(20)
2829
}
@@ -34,7 +35,7 @@ object MemberMockScenario {
3435
},
3536
scenario(page = "Main Page", name = "Member List - empty") {
3637
add {
37-
api("members") {
38+
apiDetail("members") {
3839
response {
3940
getMembers(0)
4041
}
@@ -43,7 +44,7 @@ object MemberMockScenario {
4344
},
4445
scenario(page = "Main Page", name = "Member List - Error") {
4546
add {
46-
api("members") {
47+
apiDetail("members") {
4748
response {
4849
MockBubbleManager.RESPONSE_ERROR
4950
}

app/src/main/java/tw/dcard/bubblemock/sample/api/member/MemberMockScenario2.kt

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
package tw.dcard.bubblemock.sample.api.member
22

3-
import tw.dcard.bubblemock.model.MockApi
4-
import tw.dcard.bubblemock.model.MockScenario
5-
import tw.dcard.bubblemock.model.api
6-
import tw.dcard.bubblemock.model.scenario
3+
import tw.dcard.bubblemock.model.*
74

85
object MemberMockScenario2 {
96

@@ -14,7 +11,7 @@ object MemberMockScenario2 {
1411
false
1512
}
1613
add {
17-
api("members") {
14+
apiDetail("members") {
1815
response {
1916
getMembers()
2017
}
@@ -24,7 +21,7 @@ object MemberMockScenario2 {
2421
// Regular Declaration Method
2522
MockScenario(page = "Main Page", name = "Member List - same data with address").apply {
2623
mockApiList = mutableListOf(
27-
MockApi(listOf("members")).apply {
24+
MockApi(UrlSpec.Detail(listOf("members"))).apply {
2825
responseObject = getMembersWithAddress()
2926
}
3027
)
Lines changed: 54 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
package tw.dcard.bubblemock.model
22

3+
import android.util.Log
34
import okhttp3.Request
45

56
/**
67
* @author Batu
78
*/
89
data class MockApi(
9-
private val urlSpecs: List<String>
10+
private val urlSpec: UrlSpec
1011
) {
1112

1213
companion object {
@@ -15,12 +16,13 @@ data class MockApi(
1516
}
1617

1718
var htmlMethod: String = "GET"
18-
var urlParams: MutableMap<String, String?> = mutableMapOf()
19+
1920
var delay: Long = 0L
2021
var responseObject: Any? = null
2122

2223
fun handle(request: Request): Any? {
2324
val url = request.url
25+
Log.d("badu", "url: $url")
2426
val segments = url.pathSegments
2527

2628
if (responseObject == null) {
@@ -31,28 +33,42 @@ data class MockApi(
3133
return null
3234
}
3335

34-
if (segments.size != urlSpecs.size) {
35-
return null
36-
}
37-
38-
urlSpecs.forEachIndexed { index, spec ->
39-
if (spec != ANY_PATH && spec != segments[index]) {
40-
return null
41-
}
42-
}
43-
44-
if (url.querySize != urlParams.size) {
45-
return null
46-
}
47-
48-
urlParams.forEach {
49-
val realValue = url.queryParameter(it.key)
50-
val mockValue = it.value
51-
if (mockValue == ANY_PARAM_VALUE && realValue != null) {
52-
return@forEach
36+
when (urlSpec) {
37+
is UrlSpec.Detail -> {
38+
if (segments.size != urlSpec.urlSpecs.size) {
39+
return null
40+
}
41+
42+
urlSpec.urlSpecs.forEachIndexed { index, spec ->
43+
if (spec != ANY_PATH && spec != segments[index]) {
44+
return null
45+
}
46+
}
47+
48+
if (url.querySize != urlSpec.urlParams.size) {
49+
return null
50+
}
51+
52+
urlSpec.urlParams.forEach {
53+
val realValue = url.queryParameter(it.key)
54+
val mockValue = it.value
55+
if (mockValue == ANY_PARAM_VALUE && realValue != null) {
56+
return@forEach
57+
}
58+
if (realValue != mockValue) {
59+
return null
60+
}
61+
}
5362
}
54-
if (realValue != mockValue) {
55-
return null
63+
is UrlSpec.Regex -> {
64+
if (urlSpec.isContainsMatched) {
65+
if (urlSpec.rules.toRegex().containsMatchIn(url.toString()).not())
66+
return null
67+
68+
} else {
69+
if (urlSpec.rules.toRegex().matches(url.toString()).not())
70+
return null
71+
}
5672
}
5773
}
5874

@@ -69,7 +85,7 @@ data class MockApi(
6985
fun params(init: Params.() -> Unit) {
7086
val params = Params()
7187
params.init()
72-
urlParams.putAll(params)
88+
(urlSpec as? UrlSpec.Detail)?.urlParams?.putAll(params)
7389
}
7490

7591
fun response(block: () -> Any) {
@@ -86,19 +102,29 @@ data class MockApi(
86102
}
87103
}
88104

89-
fun api(vararg urlSpecs: String, init: MockApi.() -> Unit = {}): MockApi {
105+
fun apiDetail(vararg urlSpecs: String, init: MockApi.() -> Unit = {}): MockApi {
90106
val list = mutableListOf<String>().apply {
91107
for (urlSpec in urlSpecs) {
92108
add(urlSpec)
93109
}
94110
}
95-
val api = MockApi(list)
111+
val api = MockApi(UrlSpec.Detail(list))
112+
api.init()
113+
return api
114+
}
115+
116+
fun apiDetail(urlSpecs: List<String>, init: MockApi.() -> Unit = {}): MockApi {
117+
val api = MockApi(UrlSpec.Detail(urlSpecs))
96118
api.init()
97119
return api
98120
}
99121

100-
fun api(urlSpecs: List<String>, init: MockApi.() -> Unit = {}): MockApi {
101-
val api = MockApi(urlSpecs)
122+
fun apiRegex(
123+
rule: String,
124+
isContainsMatched: Boolean = true,
125+
init: MockApi.() -> Unit = {}
126+
): MockApi {
127+
val api = MockApi(UrlSpec.Regex(rule, isContainsMatched))
102128
api.init()
103129
return api
104130
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package tw.dcard.bubblemock.model
2+
3+
/**
4+
* @author Batu
5+
*/
6+
sealed class UrlSpec{
7+
data class Detail(
8+
val urlSpecs: List<String>,
9+
var urlParams: MutableMap<String, String?> = mutableMapOf(),
10+
): UrlSpec()
11+
12+
data class Regex(
13+
val rules: String,
14+
val isContainsMatched: Boolean = true,
15+
): UrlSpec()
16+
}

0 commit comments

Comments
 (0)