Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[mockserver] Allow to set the content-type of String responses #5489

Merged
merged 8 commits into from
Dec 18, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 3 additions & 2 deletions libraries/apollo-mockserver/api/apollo-mockserver.api
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,10 @@ public final class com/apollographql/apollo3/mockserver/MockServerKt {
public static synthetic fun awaitRequest-8Mi8wO0$default (Lcom/apollographql/apollo3/mockserver/MockServer;JLkotlin/coroutines/Continuation;ILjava/lang/Object;)Ljava/lang/Object;
public static final fun enqueue (Lcom/apollographql/apollo3/mockserver/MockServer;Ljava/lang/String;JI)V
public static synthetic fun enqueue$default (Lcom/apollographql/apollo3/mockserver/MockServer;Ljava/lang/String;JIILjava/lang/Object;)V
public static final fun enqueueGraphQLString (Lcom/apollographql/apollo3/mockserver/MockServer;Ljava/lang/String;)V
public static final fun enqueueMultipart (Lcom/apollographql/apollo3/mockserver/MockServer;Ljava/util/List;)Ljava/lang/Void;
public static final fun enqueueString (Lcom/apollographql/apollo3/mockserver/MockServer;Ljava/lang/String;JI)V
public static synthetic fun enqueueString$default (Lcom/apollographql/apollo3/mockserver/MockServer;Ljava/lang/String;JIILjava/lang/Object;)V
public static final fun enqueueString (Lcom/apollographql/apollo3/mockserver/MockServer;Ljava/lang/String;JILjava/lang/String;)V
public static synthetic fun enqueueString$default (Lcom/apollographql/apollo3/mockserver/MockServer;Ljava/lang/String;JILjava/lang/String;ILjava/lang/Object;)V
}

public final class com/apollographql/apollo3/mockserver/TcpServer_concurrentKt {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -267,14 +267,23 @@ fun MockServer(handler: MockServerHandler): MockServer =
@ApolloDeprecatedSince(ApolloDeprecatedSince.Version.v4_0_0)
fun MockServer.enqueue(string: String = "", delayMs: Long = 0, statusCode: Int = 200) = enqueueString(string, delayMs, statusCode)

fun MockServer.enqueueString(string: String = "", delayMs: Long = 0, statusCode: Int = 200) {
fun MockServer.enqueueString(string: String = "", delayMs: Long = 0, statusCode: Int = 200, contentType: String = "text/plain") {
enqueue(MockResponse.Builder()
.statusCode(statusCode)
.body(string)
.addHeader("content-type", contentType)
.delayMillis(delayMs)
.build())
}

fun MockServer.enqueueGraphQLString(string: String) {
enqueue(MockResponse.Builder()
.statusCode(200)
.addHeader("content-type", "application/graphql-response+json")
.body(string)
.build())
}

@ApolloExperimental
interface MultipartBody {
fun enqueuePart(bytes: ByteString, isLast: Boolean)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.apollographql.apollo3.mockserver

import okio.Buffer
import okio.IOException

internal interface Reader {
val buffer: Buffer
Expand Down Expand Up @@ -35,7 +34,7 @@ private fun parseRequestLine(line: String): Triple<String, String, String> {
return Triple(method, match.groupValues[2], match.groupValues[3])
}

internal class ConnectionClosed(cause: Throwable?): Exception("client closed the connection", cause)
internal class ConnectionClosed(cause: Throwable?) : Exception("client closed the connection", cause)

internal suspend fun readRequest(reader: Reader): MockRequestBase {
suspend fun nextLine(): String {
Expand All @@ -62,23 +61,21 @@ internal suspend fun readRequest(reader: Reader): MockRequestBase {
return buffer2
}

/**
* Check if the client closed the connection
*/
if (reader.buffer.size == 0L) {
try {
reader.fillBuffer()
} catch (e: IOException) {
throw ConnectionClosed(e)
}
var line = try {
nextLine()
} catch (e: Exception) {
/**
* XXX: if the connection is closed in the middle of the first request line, this is detected
* as a normal connection close.
*/
throw ConnectionClosed(e)
}

var line = nextLine()

val (method, path, version) = parseRequestLine(line.trimEol())
//println("Line: ${line.trimEol()}")

val headers = mutableMapOf<String, String>()

/**
* Read headers
*/
Expand All @@ -90,11 +87,11 @@ internal suspend fun readRequest(reader: Reader): MockRequestBase {
}

val (key, value) = parseHeader(line.trimEol())
headers.put(key, value)
headers.put(key.lowercase(), value)
}

val contentLength = headers["Content-Length"]?.toLongOrNull() ?: 0
val transferEncoding = headers["Transfer-Encoding"]?.lowercase()
val contentLength = headers["content-length"]?.toLongOrNull() ?: 0
val transferEncoding = headers["transfer-encoding"]?.lowercase()
check(transferEncoding == null || transferEncoding == "identity" || transferEncoding == "chunked") {
"Transfer-Encoding $transferEncoding is not supported"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@ class ReadRequestTest {
assertEquals("/", recordedRequest.path)
assertEquals("HTTP/2", recordedRequest.version)
assertEquals(mapOf(
"Host" to "github.com",
"User-Agent" to "curl/7.64.1",
"Accept" to "*/*"
"host" to "github.com",
"user-agent" to "curl/7.64.1",
"accept" to "*/*"
), recordedRequest.headers)
assertEquals(0, (recordedRequest as MockRequest).body.size)
}
Expand Down