-
Notifications
You must be signed in to change notification settings - Fork 1k
/
MockUtils.kt
113 lines (100 loc) · 3.33 KB
/
MockUtils.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
/*
* Copyright 2014-2019 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license.
*/
package io.ktor.client.engine.mock
import io.ktor.client.request.*
import io.ktor.http.*
import io.ktor.http.content.*
import io.ktor.util.*
import io.ktor.util.date.*
import io.ktor.utils.io.*
import io.ktor.utils.io.charsets.*
import io.ktor.utils.io.core.*
import kotlinx.coroutines.*
@OptIn(DelicateCoroutinesApi::class)
@Suppress("KDocMissingDocumentation")
public suspend fun OutgoingContent.toByteArray(): ByteArray = when (this) {
is OutgoingContent.ByteArrayContent -> bytes()
is OutgoingContent.ReadChannelContent -> readFrom().toByteArray()
is OutgoingContent.WriteChannelContent -> {
val channel = ByteChannel()
GlobalScope.launch(Dispatchers.Unconfined) {
writeTo(channel)
channel.close()
}
channel.toByteArray()
}
else -> ByteArray(0)
}
@Suppress("KDocMissingDocumentation")
@OptIn(DelicateCoroutinesApi::class)
public suspend fun OutgoingContent.toByteReadPacket(): ByteReadPacket = when (this) {
is OutgoingContent.ByteArrayContent -> ByteReadPacket(bytes())
is OutgoingContent.ReadChannelContent -> readFrom().readRemaining()
is OutgoingContent.WriteChannelContent -> {
val channel = ByteChannel()
GlobalScope.launch(Dispatchers.Unconfined) {
writeTo(channel)
channel.close()
}
channel.readRemaining()
}
else -> ByteReadPacket.Empty
}
/**
* Send error response.
*/
public fun MockRequestHandleScope.respondError(
status: HttpStatusCode,
content: String = status.description,
headers: Headers = headersOf()
): HttpResponseData = respond(content, status, headers)
/**
* Send ok response.
*/
public fun MockRequestHandleScope.respondOk(
content: String = ""
): HttpResponseData = respond(content, HttpStatusCode.OK)
/**
* Respond redirect with [location] in Location header.
*/
public fun MockRequestHandleScope.respondRedirect(
location: String = ""
): HttpResponseData = respond("", HttpStatusCode.TemporaryRedirect, headersOf(HttpHeaders.Location, location))
/**
* Send [HttpStatusCode.BadRequest] response.
*/
public fun MockRequestHandleScope.respondBadRequest(): HttpResponseData =
respond("Bad Request", HttpStatusCode.BadRequest)
/**
* Send response with specified string [content], [status] and [headers].
*/
public fun MockRequestHandleScope.respond(
content: String,
status: HttpStatusCode = HttpStatusCode.OK,
headers: Headers = headersOf()
): HttpResponseData =
respond(ByteReadChannel(content.toByteArray(Charsets.UTF_8)), status, headers)
/**
* Send response with specified bytes [content], [status] and [headers].
*/
public fun MockRequestHandleScope.respond(
content: ByteArray,
status: HttpStatusCode = HttpStatusCode.OK,
headers: Headers = headersOf()
): HttpResponseData = respond(ByteReadChannel(content), status, headers)
/**
* Send response with specified [ByteReadChannel] [content], [status] and [headers].
*/
public fun MockRequestHandleScope.respond(
content: ByteReadChannel,
status: HttpStatusCode = HttpStatusCode.OK,
headers: Headers = headersOf()
): HttpResponseData = HttpResponseData(
status,
GMTDate(),
headers,
HttpProtocolVersion.HTTP_1_1,
content,
callContext
)