diff --git a/.gitignore b/.gitignore index bce2f67..d7c8359 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,6 @@ +############################## +## Swift and macOS +############################## Packages .build xcuserdata @@ -5,3 +8,72 @@ Catbird.xcodeproj DerivedData/ .DS_Store .swiftpm + +############################## +## Java +############################## +.mtj.tmp/ +*.class +*.jar +*.war +*.ear +*.nar +hs_err_pid* + +############################## +## Maven +############################## +target/ +pom.xml.tag +pom.xml.releaseBackup +pom.xml.versionsBackup +pom.xml.next +pom.xml.bak +release.properties +dependency-reduced-pom.xml +buildNumber.properties +.mvn/timing.properties +.mvn/wrapper/maven-wrapper.jar + +############################## +## Gradle +############################## +bin/ +build/ +.gradle +.gradletasknamecache +gradle-app.setting +!gradle-wrapper.jar + +############################## +## IntelliJ +############################## +out/ +.idea/ +.idea_modules/ +*.iml +*.ipr +*.iws + +############################## +## Eclipse +############################## +.settings/ +bin/ +tmp/ +.metadata +.classpath +.project +*.tmp +*.bak +*.swp +*~.nib +local.properties +.loadpath +.factorypath + +############################## +## Visual Studio Code +############################## +.vscode/ +.code-workspace diff --git a/catbird-api/build.gradle b/catbird-api/build.gradle new file mode 100644 index 0000000..cece448 --- /dev/null +++ b/catbird-api/build.gradle @@ -0,0 +1,21 @@ +plugins { + id 'java' + id 'org.jetbrains.kotlin.jvm' version '1.4.0' + id 'org.jetbrains.kotlin.plugin.serialization' version '1.4.10' +} + +apply plugin: 'kotlinx-serialization' + +group 'com.redmadrobot' +version '1.0-SNAPSHOT' + +repositories { + mavenCentral() +} + +dependencies { + implementation 'org.jetbrains.kotlin:kotlin-stdlib' + implementation 'org.jetbrains.kotlinx:kotlinx-serialization-core:1.0.0-RC' + implementation 'com.google.code.gson:gson:2.8.6' + testCompile group: 'junit', name: 'junit', version: '4.12' +} diff --git a/catbird-api/gradle.properties b/catbird-api/gradle.properties new file mode 100644 index 0000000..29e08e8 --- /dev/null +++ b/catbird-api/gradle.properties @@ -0,0 +1 @@ +kotlin.code.style=official \ No newline at end of file diff --git a/catbird-api/gradle/wrapper/gradle-wrapper.jar b/catbird-api/gradle/wrapper/gradle-wrapper.jar new file mode 100644 index 0000000..62d4c05 Binary files /dev/null and b/catbird-api/gradle/wrapper/gradle-wrapper.jar differ diff --git a/catbird-api/gradle/wrapper/gradle-wrapper.properties b/catbird-api/gradle/wrapper/gradle-wrapper.properties new file mode 100644 index 0000000..a4b4429 --- /dev/null +++ b/catbird-api/gradle/wrapper/gradle-wrapper.properties @@ -0,0 +1,5 @@ +distributionBase=GRADLE_USER_HOME +distributionPath=wrapper/dists +distributionUrl=https\://services.gradle.org/distributions/gradle-6.3-bin.zip +zipStoreBase=GRADLE_USER_HOME +zipStorePath=wrapper/dists diff --git a/catbird-api/gradlew b/catbird-api/gradlew new file mode 100755 index 0000000..fbd7c51 --- /dev/null +++ b/catbird-api/gradlew @@ -0,0 +1,185 @@ +#!/usr/bin/env sh + +# +# Copyright 2015 the original author or authors. +# +# 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 +# +# https://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. +# + +############################################################################## +## +## Gradle start up script for UN*X +## +############################################################################## + +# Attempt to set APP_HOME +# Resolve links: $0 may be a link +PRG="$0" +# Need this for relative symlinks. +while [ -h "$PRG" ] ; do + ls=`ls -ld "$PRG"` + link=`expr "$ls" : '.*-> \(.*\)$'` + if expr "$link" : '/.*' > /dev/null; then + PRG="$link" + else + PRG=`dirname "$PRG"`"/$link" + fi +done +SAVED="`pwd`" +cd "`dirname \"$PRG\"`/" >/dev/null +APP_HOME="`pwd -P`" +cd "$SAVED" >/dev/null + +APP_NAME="Gradle" +APP_BASE_NAME=`basename "$0"` + +# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"' + +# Use the maximum available, or set MAX_FD != -1 to use that value. +MAX_FD="maximum" + +warn () { + echo "$*" +} + +die () { + echo + echo "$*" + echo + exit 1 +} + +# OS specific support (must be 'true' or 'false'). +cygwin=false +msys=false +darwin=false +nonstop=false +case "`uname`" in + CYGWIN* ) + cygwin=true + ;; + Darwin* ) + darwin=true + ;; + MINGW* ) + msys=true + ;; + NONSTOP* ) + nonstop=true + ;; +esac + +CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar + + +# Determine the Java command to use to start the JVM. +if [ -n "$JAVA_HOME" ] ; then + if [ -x "$JAVA_HOME/jre/sh/java" ] ; then + # IBM's JDK on AIX uses strange locations for the executables + JAVACMD="$JAVA_HOME/jre/sh/java" + else + JAVACMD="$JAVA_HOME/bin/java" + fi + if [ ! -x "$JAVACMD" ] ; then + die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME + +Please set the JAVA_HOME variable in your environment to match the +location of your Java installation." + fi +else + JAVACMD="java" + which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. + +Please set the JAVA_HOME variable in your environment to match the +location of your Java installation." +fi + +# Increase the maximum file descriptors if we can. +if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ] ; then + MAX_FD_LIMIT=`ulimit -H -n` + if [ $? -eq 0 ] ; then + if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then + MAX_FD="$MAX_FD_LIMIT" + fi + ulimit -n $MAX_FD + if [ $? -ne 0 ] ; then + warn "Could not set maximum file descriptor limit: $MAX_FD" + fi + else + warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" + fi +fi + +# For Darwin, add options to specify how the application appears in the dock +if $darwin; then + GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" +fi + +# For Cygwin or MSYS, switch paths to Windows format before running java +if [ "$cygwin" = "true" -o "$msys" = "true" ] ; then + APP_HOME=`cygpath --path --mixed "$APP_HOME"` + CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` + + JAVACMD=`cygpath --unix "$JAVACMD"` + + # We build the pattern for arguments to be converted via cygpath + ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` + SEP="" + for dir in $ROOTDIRSRAW ; do + ROOTDIRS="$ROOTDIRS$SEP$dir" + SEP="|" + done + OURCYGPATTERN="(^($ROOTDIRS))" + # Add a user-defined pattern to the cygpath arguments + if [ "$GRADLE_CYGPATTERN" != "" ] ; then + OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" + fi + # Now convert the arguments - kludge to limit ourselves to /bin/sh + i=0 + for arg in "$@" ; do + CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` + CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option + + if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition + eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` + else + eval `echo args$i`="\"$arg\"" + fi + i=`expr $i + 1` + done + case $i in + 0) set -- ;; + 1) set -- "$args0" ;; + 2) set -- "$args0" "$args1" ;; + 3) set -- "$args0" "$args1" "$args2" ;; + 4) set -- "$args0" "$args1" "$args2" "$args3" ;; + 5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; + 6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; + 7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; + 8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; + 9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; + esac +fi + +# Escape application args +save () { + for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done + echo " " +} +APP_ARGS=`save "$@"` + +# Collect all arguments for the java command, following the shell quoting and substitution rules +eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS "\"-Dorg.gradle.appname=$APP_BASE_NAME\"" -classpath "\"$CLASSPATH\"" org.gradle.wrapper.GradleWrapperMain "$APP_ARGS" + +exec "$JAVACMD" "$@" diff --git a/catbird-api/gradlew.bat b/catbird-api/gradlew.bat new file mode 100644 index 0000000..a9f778a --- /dev/null +++ b/catbird-api/gradlew.bat @@ -0,0 +1,104 @@ +@rem +@rem Copyright 2015 the original author or authors. +@rem +@rem Licensed under the Apache License, Version 2.0 (the "License"); +@rem you may not use this file except in compliance with the License. +@rem You may obtain a copy of the License at +@rem +@rem https://www.apache.org/licenses/LICENSE-2.0 +@rem +@rem Unless required by applicable law or agreed to in writing, software +@rem distributed under the License is distributed on an "AS IS" BASIS, +@rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +@rem See the License for the specific language governing permissions and +@rem limitations under the License. +@rem + +@if "%DEBUG%" == "" @echo off +@rem ########################################################################## +@rem +@rem Gradle startup script for Windows +@rem +@rem ########################################################################## + +@rem Set local scope for the variables with windows NT shell +if "%OS%"=="Windows_NT" setlocal + +set DIRNAME=%~dp0 +if "%DIRNAME%" == "" set DIRNAME=. +set APP_BASE_NAME=%~n0 +set APP_HOME=%DIRNAME% + +@rem Resolve any "." and ".." in APP_HOME to make it shorter. +for %%i in ("%APP_HOME%") do set APP_HOME=%%~fi + +@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m" + +@rem Find java.exe +if defined JAVA_HOME goto findJavaFromJavaHome + +set JAVA_EXE=java.exe +%JAVA_EXE% -version >NUL 2>&1 +if "%ERRORLEVEL%" == "0" goto init + +echo. +echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. +echo. +echo Please set the JAVA_HOME variable in your environment to match the +echo location of your Java installation. + +goto fail + +:findJavaFromJavaHome +set JAVA_HOME=%JAVA_HOME:"=% +set JAVA_EXE=%JAVA_HOME%/bin/java.exe + +if exist "%JAVA_EXE%" goto init + +echo. +echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% +echo. +echo Please set the JAVA_HOME variable in your environment to match the +echo location of your Java installation. + +goto fail + +:init +@rem Get command-line arguments, handling Windows variants + +if not "%OS%" == "Windows_NT" goto win9xME_args + +:win9xME_args +@rem Slurp the command line arguments. +set CMD_LINE_ARGS= +set _SKIP=2 + +:win9xME_args_slurp +if "x%~1" == "x" goto execute + +set CMD_LINE_ARGS=%* + +:execute +@rem Setup the command line + +set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar + + +@rem Execute Gradle +"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% + +:end +@rem End local scope for the variables with windows NT shell +if "%ERRORLEVEL%"=="0" goto mainEnd + +:fail +rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of +rem the _cmd.exe /c_ return code! +if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 +exit /b 1 + +:mainEnd +if "%OS%"=="Windows_NT" endlocal + +:omega diff --git a/catbird-api/settings.gradle b/catbird-api/settings.gradle new file mode 100644 index 0000000..75a4657 --- /dev/null +++ b/catbird-api/settings.gradle @@ -0,0 +1,2 @@ +rootProject.name = 'catbird-api' + diff --git a/catbird-api/src/main/kotlin/Catbird.kt b/catbird-api/src/main/kotlin/Catbird.kt new file mode 100644 index 0000000..b2a85b8 --- /dev/null +++ b/catbird-api/src/main/kotlin/Catbird.kt @@ -0,0 +1,48 @@ +import java.io.OutputStreamWriter +import java.net.HttpURLConnection +import java.net.URL + +class Catbird( + var url: URL = URL(LOCAL_HOST), + var encoder: JsonEncoder = JsonEncoder() +) { + companion object { + const val LOCAL_HOST = "http://127.0.0.1:8080" + const val ACTION_PATH = "/catbird/api/mocks" + } + + fun update(mock: CatbirdMock) { + perform(CatbirdAction.Update(mock.pattern, mock.response)) + } + + fun remove(mock: CatbirdMock) { + perform(CatbirdAction.Remove(mock.pattern)) + } + + fun removeAll() { + perform(CatbirdAction.RemoveAll()) + } + + fun perform(action: CatbirdAction) { + val body = encoder.encode(action) + val url = this.url.toURI().resolve(ACTION_PATH).toURL(); + post(url, body) + } + + private fun post(url: URL, body: String) { + with(url.openConnection() as HttpURLConnection) { + requestMethod = "POST" + doOutput = true + setRequestProperty("Content-Type", "application/json") + + OutputStreamWriter(outputStream).use { + it.write(body) + it.flush() + } + + inputStream.bufferedReader().use { + print(it.readText()) + } + } + } +} diff --git a/catbird-api/src/main/kotlin/CatbirdAction.kt b/catbird-api/src/main/kotlin/CatbirdAction.kt new file mode 100644 index 0000000..b5e59c8 --- /dev/null +++ b/catbird-api/src/main/kotlin/CatbirdAction.kt @@ -0,0 +1,26 @@ +import kotlinx.serialization.SerialName +import kotlinx.serialization.Serializable + +@Serializable +sealed class CatbirdAction(val type: Type) { + @Serializable + enum class Type { + @SerialName("update") UPDATE, + @SerialName("remove") REMOVE, + @SerialName("removeAll") REMOVE_ALL + } + + @Serializable + data class Update( + val pattern: RequestPattern, + val response: ResponseMock + ): CatbirdAction(Type.UPDATE) + + @Serializable + data class Remove( + val pattern: RequestPattern + ): CatbirdAction(Type.REMOVE) + + @Serializable + class RemoveAll: CatbirdAction(Type.REMOVE_ALL) +} diff --git a/catbird-api/src/main/kotlin/CatbirdMock.kt b/catbird-api/src/main/kotlin/CatbirdMock.kt new file mode 100644 index 0000000..13de8fc --- /dev/null +++ b/catbird-api/src/main/kotlin/CatbirdMock.kt @@ -0,0 +1,4 @@ +interface CatbirdMock { + val pattern: RequestPattern + val response: ResponseMock +} \ No newline at end of file diff --git a/catbird-api/src/main/kotlin/JsonEncoder.kt b/catbird-api/src/main/kotlin/JsonEncoder.kt new file mode 100644 index 0000000..f82dd1d --- /dev/null +++ b/catbird-api/src/main/kotlin/JsonEncoder.kt @@ -0,0 +1,12 @@ +import kotlinx.serialization.encodeToString +import kotlinx.serialization.json.* + +class JsonEncoder(prettyPrint: Boolean = false) { + val json = Json { + this.prettyPrint = prettyPrint + this.classDiscriminator = "_type" + } + + inline fun encode(value: T): String = + json.encodeToString(value) +} diff --git a/catbird-api/src/main/kotlin/PatternMatch.kt b/catbird-api/src/main/kotlin/PatternMatch.kt new file mode 100644 index 0000000..efbced2 --- /dev/null +++ b/catbird-api/src/main/kotlin/PatternMatch.kt @@ -0,0 +1,30 @@ +import kotlinx.serialization.SerialName +import kotlinx.serialization.Serializable + +@Serializable +data class PatternMatch( + val kind: Kind = Kind.EQUAL, + val value: String +) { + @Serializable + enum class Kind { + @SerialName("equal") EQUAL, + @SerialName("wildcard") WILDCARD, + @SerialName("regexp") REGEXP + } + + companion object { + @JvmStatic + fun equal(value: String) = + PatternMatch(Kind.EQUAL, value) + + @JvmStatic + fun wildcard(value: String) = + PatternMatch(Kind.WILDCARD, value) + + @JvmStatic + fun regexp(value: String) = + PatternMatch(Kind.REGEXP, value) + } +} + diff --git a/catbird-api/src/main/kotlin/RequestPattern.kt b/catbird-api/src/main/kotlin/RequestPattern.kt new file mode 100644 index 0000000..62a5a7d --- /dev/null +++ b/catbird-api/src/main/kotlin/RequestPattern.kt @@ -0,0 +1,20 @@ +import kotlinx.serialization.Serializable + +@Serializable +data class RequestPattern( + var method: String = "GET", + var url: PatternMatch = PatternMatch(value = ""), + var headers: MutableMap = mutableMapOf() +) { + fun setPath(string: String) { + url = PatternMatch(value = string) + } + + fun setHeader(name: String, value: String) { + headers[name] = PatternMatch(value = value) + } + + fun setCookie(value: String) { + setHeader("Cookie", value) + } +} \ No newline at end of file diff --git a/catbird-api/src/main/kotlin/ResponseMock.kt b/catbird-api/src/main/kotlin/ResponseMock.kt new file mode 100644 index 0000000..9598c04 --- /dev/null +++ b/catbird-api/src/main/kotlin/ResponseMock.kt @@ -0,0 +1,31 @@ +import kotlinx.serialization.Serializable +import java.nio.file.Files +import java.nio.file.Paths +import java.util.Base64 + +@Serializable +data class ResponseMock( + var status: Int = 200, + var headers: MutableMap = mutableMapOf(), + var body: String? = null, + var limit: Int? = null, + var delay: Int? = null, +) { + fun setHeader(name: String, value: String) { + headers[name] = value + } + + fun setCookie(value: String) { + setHeader("Set-Cookie", value) + } + + fun setFile(path: String) { + val fileBytes = Files.readAllBytes(Paths.get(path)) + val encodedBytes = Base64.getEncoder().encode(fileBytes) + body = String(encodedBytes) + } + + fun setString(string: String) { + body = Base64.getEncoder().encodeToString(string.toByteArray()) + } +} \ No newline at end of file diff --git a/catbird-api/src/test/kotlin/CatbirdActionTest.kt b/catbird-api/src/test/kotlin/CatbirdActionTest.kt new file mode 100644 index 0000000..3719ad8 --- /dev/null +++ b/catbird-api/src/test/kotlin/CatbirdActionTest.kt @@ -0,0 +1,71 @@ +import org.junit.Assert.assertEquals +import org.junit.Test + +class CatbirdActionTest { + private val encoder = JsonEncoder(prettyPrint = true) + + @Test + fun `encode update action`() { + val pattern = RequestPattern() + val response = ResponseMock() + val action = CatbirdAction.Update(pattern, response) + val actual = encoder.encode(action) + var expected = """ + { + "type": "update", + "pattern": { + "method": "GET", + "url": { + "kind": "equal", + "value": "" + }, + "headers": { + } + }, + "response": { + "status": 200, + "headers": { + }, + "body": null, + "limit": null, + "delay": null + } + } + """.trimIndent() + assertEquals(expected, actual) + } + + @Test + fun `encode remove action`() { + val pattern = RequestPattern() + val action = CatbirdAction.Remove(pattern) + val actual = encoder.encode(action) + var expected = """ + { + "type": "remove", + "pattern": { + "method": "GET", + "url": { + "kind": "equal", + "value": "" + }, + "headers": { + } + } + } + """.trimIndent() + assertEquals(expected, actual) + } + + @Test + fun `encode remove all action`() { + val action = CatbirdAction.RemoveAll() + val actual = encoder.encode(action) + var expected = """ + { + "type": "removeAll" + } + """.trimIndent() + assertEquals(expected, actual) + } +} diff --git a/catbird-api/src/test/kotlin/PatternMatchTest.kt b/catbird-api/src/test/kotlin/PatternMatchTest.kt new file mode 100644 index 0000000..3362f75 --- /dev/null +++ b/catbird-api/src/test/kotlin/PatternMatchTest.kt @@ -0,0 +1,60 @@ +import org.junit.Assert.assertEquals +import org.junit.Test + +class PatternMatchTest { + private val encoder = JsonEncoder(prettyPrint = true) + + @Test + fun `default kind equal`() { + val pattern = PatternMatch(value = "accounts") + assertEquals(PatternMatch.Kind.EQUAL, pattern.kind) + } + + @Test + fun `encode equal to json`() { + val pattern = PatternMatch( + PatternMatch.Kind.EQUAL, + "accounts" + ) + val actual = encoder.encode(pattern) + var expected = """ + { + "kind": "equal", + "value": "accounts" + } + """.trimIndent() + assertEquals(expected, actual) + } + + @Test + fun `encode wildcard to json`() { + val pattern = PatternMatch( + PatternMatch.Kind.WILDCARD, + "accounts" + ) + val actual = encoder.encode(pattern) + var expected = """ + { + "kind": "wildcard", + "value": "accounts" + } + """.trimIndent() + assertEquals(expected, actual) + } + + @Test + fun `encode regexp to json`() { + val pattern = PatternMatch( + PatternMatch.Kind.REGEXP, + "accounts" + ) + val actual = encoder.encode(pattern) + var expected = """ + { + "kind": "regexp", + "value": "accounts" + } + """.trimIndent() + assertEquals(expected, actual) + } +} \ No newline at end of file diff --git a/catbird-api/src/test/kotlin/RequestPatternTest.kt b/catbird-api/src/test/kotlin/RequestPatternTest.kt new file mode 100644 index 0000000..b5f61f4 --- /dev/null +++ b/catbird-api/src/test/kotlin/RequestPatternTest.kt @@ -0,0 +1,83 @@ +import org.junit.Assert.assertEquals +import org.junit.Test + +class RequestPatternTest { + private val encoder = JsonEncoder(prettyPrint = true) + + @Test + fun `default values`() { + val actual = RequestPattern() + val expected = RequestPattern( + method = "GET", + url = PatternMatch.equal(""), + headers = mutableMapOf() + ) + assertEquals(expected, actual) + } + + @Test + fun `set header`() { + val request = RequestPattern() + request.setHeader("a", "b") + request.setHeader("1", "2") + val actual = request.headers + val expected = mapOf( + "a" to PatternMatch.equal("b"), + "1" to PatternMatch.equal("2") + ) + assertEquals(expected, actual) + } + + @Test + fun `set cookie`() { + val request = RequestPattern() + request.setCookie("x") + val actual = request.headers + val expected = mapOf("Cookie" to PatternMatch.equal("x")) + assertEquals(expected, actual) + } + + @Test + fun `encode with default values`() { + val request = RequestPattern() + val actual = encoder.encode(request) + val expected = """ + { + "method": "GET", + "url": { + "kind": "equal", + "value": "" + }, + "headers": { + } + } + """.trimIndent() + assertEquals(expected, actual) + } + + @Test + fun `encode with all values`() { + val request = RequestPattern( + method = "POST", + url = PatternMatch(value = "/accounts"), + headers = mutableMapOf("z" to PatternMatch.wildcard("*")) + ) + val actual = encoder.encode(request) + val expected = """ + { + "method": "POST", + "url": { + "kind": "equal", + "value": "/accounts" + }, + "headers": { + "z": { + "kind": "wildcard", + "value": "*" + } + } + } + """.trimIndent() + assertEquals(expected, actual) + } +} \ No newline at end of file diff --git a/catbird-api/src/test/kotlin/ResponseMockTest.kt b/catbird-api/src/test/kotlin/ResponseMockTest.kt new file mode 100644 index 0000000..46b25be --- /dev/null +++ b/catbird-api/src/test/kotlin/ResponseMockTest.kt @@ -0,0 +1,75 @@ +import org.junit.Assert.assertEquals +import org.junit.Test + +class ResponseMockTest { + private val encoder = JsonEncoder(prettyPrint = true) + + @Test + fun `default values`() { + val actual = ResponseMock() + val expected = ResponseMock( + status = 200, + headers = mutableMapOf(), + body = null, + limit = null, + delay = null + ) + assertEquals(expected, actual) + } + + @Test + fun `set header`() { + val response = ResponseMock() + response.setHeader("a", "b") + response.setHeader("1", "2") + assertEquals(mapOf("a" to "b", "1" to "2"), response.headers) + } + + @Test + fun `set cookie`() { + val response = ResponseMock() + response.setCookie("x") + assertEquals(mapOf("Set-Cookie" to "x"), response.headers) + } + + @Test + fun `encode with default values`() { + val response = ResponseMock() + val actual = encoder.encode(response) + val expected = """ + { + "status": 200, + "headers": { + }, + "body": null, + "limit": null, + "delay": null + } + """.trimIndent() + assertEquals(expected, actual) + } + + @Test + fun `encode with all values`() { + val response = ResponseMock( + status = 500, + headers = mutableMapOf("x" to "y"), + body = "hello", + limit = 1, + delay = 4 + ) + val actual = encoder.encode(response) + val expected = """ + { + "status": 500, + "headers": { + "x": "y" + }, + "body": "hello", + "limit": 1, + "delay": 4 + } + """.trimIndent() + assertEquals(expected, actual) + } +} \ No newline at end of file