Skip to content

Commit ef39815

Browse files
Add shared code with MockServer, use in samples
1 parent 8a5195e commit ef39815

File tree

16 files changed

+196
-27
lines changed

16 files changed

+196
-27
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.

sample-android/build.gradle

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ android {
2828
packagingOptions {
2929
exclude 'META-INF/LICENSE.md'
3030
exclude 'META-INF/LICENSE-notice.md'
31+
exclude 'META-INF/kotlinx-serialization-runtime.kotlin_module'
3132
}
3233
}
3334

@@ -49,11 +50,7 @@ dependencies {
4950
implementation 'androidx.recyclerview:recyclerview-selection:1.0.0'
5051

5152
implementation project(':mockttp-core')
53+
implementation project(':sample-sharedCode')
5254

5355
testImplementation 'junit:junit:4.13'
54-
55-
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
56-
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
57-
58-
// androidTestImplementation project(':sample-sharedTests')
5956
}

sample-android/src/main/kotlin/ContributorsRepository.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import java.lang.Exception
1414
data class Model(val login: String, val contributions: Int)
1515

1616
enum class Environment {
17-
MOCKED, ORIGINAL
17+
MOCKED, SHARED_MOCK, ORIGINAL
1818
}
1919

2020
class ContributorsRepository(val environment: Environment) {
@@ -23,6 +23,7 @@ class ContributorsRepository(val environment: Environment) {
2323
private fun url(): String {
2424
val baseUrl = when(environment) {
2525
Environment.MOCKED -> "http://localhost:8080"
26+
Environment.SHARED_MOCK -> "http://localhost:8081"
2627
Environment.ORIGINAL -> "https://api.github.com"
2728
}
2829
return "$baseUrl/repos/michallaskowski/kuiks/contributors"

sample-android/src/main/kotlin/MainActivity.kt

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,16 @@ import androidx.appcompat.app.AppCompatActivity
66
import android.view.Menu
77
import android.view.MenuItem
88
import androidx.lifecycle.lifecycleScope
9-
import dev.michallaskowski.mockttp.HttpServer
10-
import dev.michallaskowski.mockttp.Request
11-
import dev.michallaskowski.mockttp.Response
12-
import dev.michallaskowski.mockttp.Router
9+
import dev.michallaskowski.mockttp.*
10+
import dev.michallaskowski.mockttp.sample.shared.MockServer
1311

1412
import kotlinx.android.synthetic.main.activity_main.*
1513
import kotlinx.android.synthetic.main.content_main.*
1614
import kotlinx.coroutines.Dispatchers
1715
import kotlinx.coroutines.launch
18-
import kotlinx.serialization.Serializable
1916
import kotlinx.serialization.builtins.list
2017
import kotlinx.serialization.json.Json
2118
import kotlinx.serialization.json.JsonConfiguration
22-
import okio.Buffer
2319

2420
class MainActivity : AppCompatActivity() {
2521

@@ -35,6 +31,10 @@ class MainActivity : AppCompatActivity() {
3531
selectedEnv = Environment.MOCKED
3632
startMockServer()
3733
}
34+
environment_shared_mock.id -> {
35+
selectedEnv = Environment.SHARED_MOCK
36+
startCommonMockServer()
37+
}
3838
else -> selectedEnv = Environment.ORIGINAL
3939
}
4040
val goToListIntent = Intent(this, ContributorsActivity::class.java)
@@ -43,10 +43,6 @@ class MainActivity : AppCompatActivity() {
4343
}
4444
}
4545

46-
private fun navigate() {
47-
48-
}
49-
5046
override fun onCreateOptionsMenu(menu: Menu): Boolean {
5147
// Inflate the menu; this adds items to the action bar if it is present.
5248
menuInflater.inflate(R.menu.menu_main, menu)
@@ -64,6 +60,7 @@ class MainActivity : AppCompatActivity() {
6460
}
6561

6662
private var httpServer: HttpServer? = null
63+
private var commonHttpServer: MockServer? = null
6764

6865
private fun startMockServer() {
6966
if (httpServer != null) {
@@ -77,15 +74,26 @@ class MainActivity : AppCompatActivity() {
7774
httpServer?.start(8080)
7875
}
7976
}
77+
78+
private fun startCommonMockServer() {
79+
if (commonHttpServer != null) {
80+
return
81+
}
82+
83+
commonHttpServer = MockServer()
84+
lifecycleScope.launch(Dispatchers.Default) {
85+
commonHttpServer?.start(8081)
86+
}
87+
}
8088
}
8189

8290
private class MockingRouter: Router {
8391
override fun handleRequest(request: Request): Response {
8492
if (request.method == "GET" && request.path?.startsWith("/repos/") == true) {
85-
val data = Json(JsonConfiguration.Default).stringify(
93+
val data = Json(JsonConfiguration.Stable).stringify(
8694
Model.serializer().list,
8795
listOf(Model("test", 42)))
88-
return Response(200, emptyMap(), Buffer().writeUtf8(data), "application/json")
96+
return Response(200, emptyMap(), Data(data), "application/json")
8997
} else {
9098
return Response(404, emptyMap(), null, null)
9199
}

sample-android/src/main/res/layout/content_main.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,12 @@
2525
android:layout_height="wrap_content"
2626
android:text="Mocked" />
2727

28+
<RadioButton
29+
android:id="@+id/environment_shared_mock"
30+
android:layout_width="wrap_content"
31+
android:layout_height="wrap_content"
32+
android:text="Shared Mock" />
33+
2834
<RadioButton
2935
android:id="@+id/environment_original"
3036
android:layout_width="wrap_content"

sample-ios/SampleiOS.xcodeproj/project.pbxproj

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
C17070A6241709CA00A35D8A /* mockttp.framework in CopyFiles */ = {isa = PBXBuildFile; fileRef = C17070A3241708AB00A35D8A /* mockttp.framework */; settings = {ATTRIBUTES = (CodeSignOnCopy, RemoveHeadersOnCopy, ); }; };
2525
C17070A8241709EC00A35D8A /* GCDWebServers.framework in CopyFiles */ = {isa = PBXBuildFile; fileRef = C17070A7241709EC00A35D8A /* GCDWebServers.framework */; settings = {ATTRIBUTES = (CodeSignOnCopy, RemoveHeadersOnCopy, ); }; };
2626
C17070AA2422D74300A35D8A /* ContributorsView.swift in Sources */ = {isa = PBXBuildFile; fileRef = C17070A92422D74300A35D8A /* ContributorsView.swift */; };
27+
C17E09C524AF756700BB0A33 /* sharedMock.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = C17E09C424AF756700BB0A33 /* sharedMock.framework */; };
28+
C17E09C624AF756700BB0A33 /* sharedMock.framework in CopyFiles */ = {isa = PBXBuildFile; fileRef = C17E09C424AF756700BB0A33 /* sharedMock.framework */; settings = {ATTRIBUTES = (CodeSignOnCopy, RemoveHeadersOnCopy, ); }; };
2729
/* End PBXBuildFile section */
2830

2931
/* Begin PBXContainerItemProxy section */
@@ -53,6 +55,7 @@
5355
dstPath = "";
5456
dstSubfolderSpec = 10;
5557
files = (
58+
C17E09C624AF756700BB0A33 /* sharedMock.framework in CopyFiles */,
5659
C17070A8241709EC00A35D8A /* GCDWebServers.framework in CopyFiles */,
5760
C17070A6241709CA00A35D8A /* mockttp.framework in CopyFiles */,
5861
);
@@ -82,6 +85,7 @@
8285
C17070A3241708AB00A35D8A /* mockttp.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = mockttp.framework; path = ../sharedCode/build/bin/iosX64/debugFramework/mockttp.framework; sourceTree = "<group>"; };
8386
C17070A7241709EC00A35D8A /* GCDWebServers.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = GCDWebServers.framework; path = ../Carthage/Build/iOS/GCDWebServers.framework; sourceTree = "<group>"; };
8487
C17070A92422D74300A35D8A /* ContributorsView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ContributorsView.swift; sourceTree = "<group>"; };
88+
C17E09C424AF756700BB0A33 /* sharedMock.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = sharedMock.framework; path = "../sample-sharedCode/build/bin/iosX64/debugFramework/sharedMock.framework"; sourceTree = "<group>"; };
8589
C1E7FC7223F3448900B11BA3 /* SampleiOS.xctestplan */ = {isa = PBXFileReference; lastKnownFileType = text; name = SampleiOS.xctestplan; path = SampleiOS.xcodeproj/SampleiOS.xctestplan; sourceTree = "<group>"; };
8690
/* End PBXFileReference section */
8791

@@ -91,6 +95,7 @@
9195
buildActionMask = 2147483647;
9296
files = (
9397
C17070A4241708AB00A35D8A /* mockttp.framework in Frameworks */,
98+
C17E09C524AF756700BB0A33 /* sharedMock.framework in Frameworks */,
9499
);
95100
runOnlyForDeploymentPostprocessing = 0;
96101
};
@@ -167,6 +172,7 @@
167172
C157091A23EF604F003481FB /* Frameworks */ = {
168173
isa = PBXGroup;
169174
children = (
175+
C17E09C424AF756700BB0A33 /* sharedMock.framework */,
170176
C17070A3241708AB00A35D8A /* mockttp.framework */,
171177
C157091B23EF6050003481FB /* sharedTests.framework */,
172178
);
@@ -309,7 +315,7 @@
309315
);
310316
runOnlyForDeploymentPostprocessing = 0;
311317
shellPath = /bin/bash;
312-
shellScript = "cd \"$PROJECT_DIR/..\"\n./gradlew mockttp-core:linkDebugFrameworkIosX64\n";
318+
shellScript = "cd \"$PROJECT_DIR/..\"\n./gradlew mockttp-core:linkDebugFrameworkIosX64 sample-sharedCode:linkDebugFrameworkIosX64\n";
313319
};
314320
/* End PBXShellScriptBuildPhase section */
315321

@@ -481,7 +487,10 @@
481487
DEVELOPMENT_ASSET_PATHS = "\"SampleiOS/Preview Content\"";
482488
DEVELOPMENT_TEAM = A3ZT87L5Y5;
483489
ENABLE_PREVIEWS = YES;
484-
FRAMEWORK_SEARCH_PATHS = "$(PROJECT_DIR)/../sharedCode/build/bin/iOSX64/debugFramework";
490+
FRAMEWORK_SEARCH_PATHS = (
491+
"$(PROJECT_DIR)/../sharedCode/build/bin/iOSX64/debugFramework",
492+
"$(PROJECT_DIR)/../sample-sharedCode/build/bin/iOSX64/debugFramework",
493+
);
485494
INFOPLIST_FILE = SampleiOS/Info.plist;
486495
LD_RUNPATH_SEARCH_PATHS = (
487496
"$(inherited)",
@@ -502,7 +511,10 @@
502511
DEVELOPMENT_ASSET_PATHS = "\"SampleiOS/Preview Content\"";
503512
DEVELOPMENT_TEAM = A3ZT87L5Y5;
504513
ENABLE_PREVIEWS = YES;
505-
FRAMEWORK_SEARCH_PATHS = "$(PROJECT_DIR)/../sharedCode/build/bin/iOSX64/debugFramework";
514+
FRAMEWORK_SEARCH_PATHS = (
515+
"$(PROJECT_DIR)/../sharedCode/build/bin/iOSX64/debugFramework",
516+
"$(PROJECT_DIR)/../sample-sharedCode/build/bin/iOSX64/debugFramework",
517+
);
506518
INFOPLIST_FILE = SampleiOS/Info.plist;
507519
LD_RUNPATH_SEARCH_PATHS = (
508520
"$(inherited)",

sample-ios/SampleiOS/ContentView.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import SwiftUI
1313

1414
enum Environment: String, Hashable {
1515
case mocked = "http://localhost:8080"
16+
case sharedMock = "http://localhost:8081"
1617
case original = "http://api.github.com"
1718
}
1819

@@ -24,6 +25,7 @@ struct ContentView: View {
2425
VStack {
2526
Picker(selection: $environment, label: Text("Choose environment")) {
2627
Text("mocked").tag(Environment.mocked)
28+
Text("shared mock").tag(Environment.sharedMock)
2729
Text("original").tag(Environment.original)
2830
}.pickerStyle(SegmentedPickerStyle())
2931

sample-ios/SampleiOS/SceneDelegate.swift

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,14 @@
99
import UIKit
1010
import SwiftUI
1111
import mockttp
12+
import sharedMock
1213

1314
class SceneDelegate: UIResponder, UIWindowSceneDelegate {
1415

1516
var window: UIWindow?
1617

1718
private var mockServer: HttpServer?
19+
private var commonMockServer: MockServer?
1820

1921
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
2022
// Use this method to optionally configure and attach the UIWindow `window` to the provided UIWindowScene `scene`.
@@ -54,9 +56,15 @@ class SceneDelegate: UIResponder, UIWindowSceneDelegate {
5456
}
5557

5658
private func presentContributors(in environment: Environment) {
57-
if environment == .mocked {
59+
switch environment {
60+
case .mocked:
5861
setupMockServer()
62+
case .sharedMock:
63+
setupCommonMockServer()
64+
case .original:
65+
break
5966
}
67+
6068
let contributorsView = ContributorsView(environment: environment)
6169
let contributorsViewController = UIHostingController(rootView: contributorsView)
6270
self.window?.rootViewController?.present(contributorsViewController, animated: true, completion: nil)
@@ -71,6 +79,15 @@ class SceneDelegate: UIResponder, UIWindowSceneDelegate {
7179
mockServer?.router = MockingRouter()
7280
mockServer?.start(port: 8080)
7381
}
82+
83+
private func setupCommonMockServer() {
84+
guard commonMockServer == nil else {
85+
return
86+
}
87+
88+
commonMockServer = MockServer()
89+
commonMockServer?.start(port: 8081)
90+
}
7491
}
7592

7693
private final class MockingRouter: Router {

sample-sharedCode/build.gradle

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
apply plugin: 'kotlin-multiplatform'
2+
apply plugin: 'com.android.library'
3+
apply plugin: 'kotlinx-serialization'
4+
5+
android {
6+
compileSdkVersion 29
7+
defaultConfig {
8+
minSdkVersion 24
9+
targetSdkVersion 29
10+
versionCode 1
11+
versionName "1.0"
12+
}
13+
buildTypes {
14+
release {
15+
minifyEnabled false
16+
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
17+
}
18+
}
19+
buildToolsVersion = '29.0.2'
20+
21+
sourceSets {
22+
getByName("main") {
23+
manifest.srcFile("src/androidMain/AndroidManifest.xml")
24+
java.srcDirs("src/androidMain/kotlin")
25+
res.srcDirs("src/androidMain/res")
26+
}
27+
}
28+
}
29+
30+
kotlin {
31+
targets {
32+
ios('ios') {
33+
binaries {
34+
framework {
35+
baseName = "sharedMock"
36+
embedBitcode("disable")
37+
linkerOpts("-F$projectDir/../Carthage/Build/iOS")
38+
}
39+
}
40+
}
41+
42+
fromPreset(presets.android, 'android')
43+
}
44+
45+
sourceSets {
46+
commonMain.dependencies {
47+
implementation 'org.jetbrains.kotlin:kotlin-stdlib-common'
48+
implementation "org.jetbrains.kotlinx:kotlinx-serialization-runtime-common:0.20.0"
49+
implementation project(':mockttp-core')
50+
}
51+
52+
iosMain {
53+
dependencies {
54+
implementation "org.jetbrains.kotlinx:kotlinx-serialization-runtime-native:0.20.0"
55+
}
56+
}
57+
58+
androidMain {
59+
dependencies {
60+
implementation "org.jetbrains.kotlinx:kotlinx-serialization-runtime:0.20.0"
61+
62+
}
63+
}
64+
}
65+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<manifest package="dev.michallaskowski.mockttp.sample.shared">
3+
<application/>
4+
</manifest>

0 commit comments

Comments
 (0)