Skip to content

Commit

Permalink
complete
Browse files Browse the repository at this point in the history
  • Loading branch information
gnarea committed Jul 11, 2021
1 parent a6f34a7 commit 1cca916
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 50 deletions.
6 changes: 3 additions & 3 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile

buildscript {
ext.kotlinVersion = '1.5.0'
ext.kotlinCoroutinesVersion = '1.4.3-native-mt'
ext.ktorVersion = '1.5.0'
ext.kotlinVersion = '1.5.20'
ext.kotlinCoroutinesVersion = '1.5.0'
ext.ktorVersion = '1.6.1'
ext.okhttpVersion = '4.9.1'
}

Expand Down
20 changes: 9 additions & 11 deletions src/test/kotlin/tech/relaycorp/poweb/ParcelDeliveryTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ import io.ktor.client.request.HttpRequestData
import io.ktor.http.HttpMethod
import io.ktor.http.HttpStatusCode
import io.ktor.http.content.OutgoingContent
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.test.runBlockingTest
import kotlinx.coroutines.runBlocking
import org.bouncycastle.util.encoders.Base64
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.assertThrows
Expand All @@ -23,14 +22,13 @@ import kotlin.test.assertEquals
import kotlin.test.assertNotNull
import kotlin.test.assertTrue

@ExperimentalCoroutinesApi
class ParcelDeliveryTest {
private val parcelSerialized = "Let's say I'm the serialization of a parcel".toByteArray()
private val signer =
Signer(PDACertPath.PRIVATE_ENDPOINT, KeyPairSet.PRIVATE_ENDPOINT.private)

@Test
fun `Request should be made with HTTP POST`() = runBlockingTest {
fun `Request should be made with HTTP POST`() = runBlocking {
var method: HttpMethod? = null
val client = makeTestClient { request: HttpRequestData ->
method = request.method
Expand All @@ -43,7 +41,7 @@ class ParcelDeliveryTest {
}

@Test
fun `Endpoint should be the one for parcels`() = runBlockingTest {
fun `Endpoint should be the one for parcels`() = runBlocking {
var endpointURL: String? = null
val client = makeTestClient { request: HttpRequestData ->
endpointURL = request.url.toString()
Expand All @@ -56,7 +54,7 @@ class ParcelDeliveryTest {
}

@Test
fun `Request content type should be the appropriate value`() = runBlockingTest {
fun `Request content type should be the appropriate value`() = runBlocking {
var contentType: String? = null
val client = makeTestClient { request: HttpRequestData ->
contentType = request.body.contentType.toString()
Expand All @@ -69,7 +67,7 @@ class ParcelDeliveryTest {
}

@Test
fun `Request body should be the parcel serialized`() = runBlockingTest {
fun `Request body should be the parcel serialized`() = runBlocking {
var requestBody: ByteArray? = null
val client = makeTestClient { request: HttpRequestData ->
assertTrue(request.body is OutgoingContent.ByteArrayContent)
Expand All @@ -83,7 +81,7 @@ class ParcelDeliveryTest {
}

@Test
fun `Delivery signature should be in the request headers`() = runBlockingTest {
fun `Delivery signature should be in the request headers`(): Unit = runBlocking {
var authorizationHeader: String? = null
val client = makeTestClient { request: HttpRequestData ->
authorizationHeader = request.headers["Authorization"]
Expand All @@ -104,7 +102,7 @@ class ParcelDeliveryTest {
}

@Test
fun `HTTP 20X should be regarded a successful delivery`() = runBlockingTest {
fun `HTTP 20X should be regarded a successful delivery`() = runBlocking {
val client = makeTestClient { respond("", HttpStatusCode.Accepted) }

client.use { client.deliverParcel(parcelSerialized, signer) }
Expand All @@ -116,7 +114,7 @@ class ParcelDeliveryTest {

client.use {
val exception = assertThrows<RejectedParcelException> {
runBlockingTest { client.deliverParcel(parcelSerialized, signer) }
runBlocking { client.deliverParcel(parcelSerialized, signer) }
}

assertEquals("The server rejected the parcel", exception.message)
Expand All @@ -130,7 +128,7 @@ class ParcelDeliveryTest {

client.use {
val exception = assertThrows<ClientBindingException> {
runBlockingTest { client.deliverParcel(parcelSerialized, signer) }
runBlocking { client.deliverParcel(parcelSerialized, signer) }
}

assertEquals("The server returned a $status response", exception.message)
Expand Down
27 changes: 12 additions & 15 deletions src/test/kotlin/tech/relaycorp/poweb/PoWebClientTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,7 @@ import io.ktor.http.HttpStatusCode
import io.ktor.http.content.ByteArrayContent
import io.ktor.http.content.OutgoingContent
import io.ktor.util.InternalAPI
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.test.runBlockingTest
import org.junit.jupiter.api.Nested
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.assertThrows
Expand All @@ -35,7 +33,6 @@ import kotlin.test.assertFalse
import kotlin.test.assertNull
import kotlin.test.assertTrue

@ExperimentalCoroutinesApi
@Suppress("RedundantInnerClassModifier")
class PoWebClientTest {
@Nested
Expand Down Expand Up @@ -163,7 +160,7 @@ class PoWebClientTest {
@Nested
inner class Request {
@Test
fun `Request should be made with HTTP POST`() = runBlockingTest {
fun `Request should be made with HTTP POST`() = runBlocking {
var method: HttpMethod? = null
val client = makeTestClient { request: HttpRequestData ->
method = request.method
Expand All @@ -176,7 +173,7 @@ class PoWebClientTest {
}

@Test
fun `Specified path should be honored`() = runBlockingTest {
fun `Specified path should be honored`() = runBlocking {
var endpointURL: String? = null
val client = makeTestClient { request: HttpRequestData ->
endpointURL = request.url.toString()
Expand All @@ -189,7 +186,7 @@ class PoWebClientTest {
}

@Test
fun `Specified Content-Type should be honored`() = runBlockingTest {
fun `Specified Content-Type should be honored`() = runBlocking {
var contentType: String? = null
val client = makeTestClient { request: HttpRequestData ->
contentType = request.body.contentType.toString()
Expand All @@ -202,7 +199,7 @@ class PoWebClientTest {
}

@Test
fun `Request body should be the parcel serialized`() = runBlockingTest {
fun `Request body should be the parcel serialized`() = runBlocking {
var requestBody: ByteArray? = null
val client = makeTestClient { request: HttpRequestData ->
assertTrue(request.body is OutgoingContent.ByteArrayContent)
Expand All @@ -216,7 +213,7 @@ class PoWebClientTest {
}

@Test
fun `No Authorization header should be set by default`() = runBlockingTest {
fun `No Authorization header should be set by default`() = runBlocking {
var authorizationHeader: String? = null
val client = makeTestClient { request: HttpRequestData ->
authorizationHeader = request.headers["Authorization"]
Expand All @@ -229,7 +226,7 @@ class PoWebClientTest {
}

@Test
fun `Authorization should be set if requested`() = runBlockingTest {
fun `Authorization should be set if requested`() = runBlocking {
val expectedAuthorizationHeader = "Foo bar"
var actualAuthorizationHeader: String? = null
val client = makeTestClient { request: HttpRequestData ->
Expand All @@ -246,7 +243,7 @@ class PoWebClientTest {
@Nested
inner class Response {
@Test
fun `HTTP 20X should be regarded a successful delivery`() = runBlockingTest {
fun `HTTP 20X should be regarded a successful delivery`(): Unit = runBlocking {
val client = makeTestClient { respond("", HttpStatusCode.Accepted) }

client.use { client.post(path, body) }
Expand All @@ -258,7 +255,7 @@ class PoWebClientTest {

client.use {
val exception = assertThrows<ServerBindingException> {
runBlockingTest { client.post(path, body) }
runBlocking { client.post(path, body) }
}

assertEquals(
Expand All @@ -275,7 +272,7 @@ class PoWebClientTest {

client.use {
val exception = assertThrows<PoWebClientException> {
runBlockingTest { client.post(path, body) }
runBlocking { client.post(path, body) }
}

assertEquals(status, exception.responseStatus)
Expand All @@ -288,7 +285,7 @@ class PoWebClientTest {

client.use {
val exception = assertThrows<ServerConnectionException> {
runBlockingTest { client.post(path, body) }
runBlocking { client.post(path, body) }
}

assertEquals(
Expand Down Expand Up @@ -378,14 +375,14 @@ class PoWebClientTest {
}

@Test
fun `Client should use WS if TLS is not required`() = runBlockingTest {
fun `Client should use WS if TLS is not required`() = runBlocking {
val client = PoWebClient(hostName, mockWebServer.port, false)

assertTrue(client.baseWsUrl.startsWith("ws:"), "Actual URL: ${client.baseWsUrl}")
}

@Test
fun `Client should use WSS if TLS is required`() = runBlockingTest {
fun `Client should use WSS if TLS is required`() = runBlocking {
val client = PoWebClient(hostName, mockWebServer.port, true)

assertTrue(client.baseWsUrl.startsWith("wss:"))
Expand Down
38 changes: 17 additions & 21 deletions src/test/kotlin/tech/relaycorp/poweb/RegistrationTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@ import io.ktor.http.HttpMethod
import io.ktor.http.HttpStatusCode
import io.ktor.http.content.OutgoingContent
import io.ktor.http.headersOf
import io.ktor.util.KtorExperimentalAPI
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.test.runBlockingTest
import kotlinx.coroutines.runBlocking
import org.junit.jupiter.api.Nested
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.assertThrows
Expand All @@ -25,8 +23,6 @@ import kotlin.test.assertEquals
import kotlin.test.assertTrue

@Suppress("RedundantInnerClassModifier")
@ExperimentalCoroutinesApi
@KtorExperimentalAPI
class RegistrationTest {
@Nested
inner class PreRegistration {
Expand All @@ -35,7 +31,7 @@ class RegistrationTest {
headersOf("Content-Type", ContentTypes.NODE_REGISTRATION_AUTHORIZATION.value)

@Test
fun `Request method should be POST`() = runBlockingTest {
fun `Request method should be POST`() = runBlocking {
var method: HttpMethod? = null
val client = makeTestClient { request: HttpRequestData ->
method = request.method
Expand All @@ -48,7 +44,7 @@ class RegistrationTest {
}

@Test
fun `Request should be made to the appropriate endpoint`() = runBlockingTest {
fun `Request should be made to the appropriate endpoint`() = runBlocking {
var endpointURL: String? = null
val client = makeTestClient { request: HttpRequestData ->
endpointURL = request.url.toString()
Expand All @@ -61,7 +57,7 @@ class RegistrationTest {
}

@Test
fun `Request Content-Type should be plain text`() = runBlockingTest {
fun `Request Content-Type should be plain text`() = runBlocking {
var contentType: ContentType? = null
val client = makeTestClient { request: HttpRequestData ->
contentType = request.body.contentType
Expand All @@ -74,7 +70,7 @@ class RegistrationTest {
}

@Test
fun `Request body should be SHA-256 digest of the node public key`() = runBlockingTest {
fun `Request body should be SHA-256 digest of the node public key`() = runBlocking {
var requestBody: ByteArray? = null
val client = makeTestClient { request: HttpRequestData ->
assertTrue(request.body is OutgoingContent.ByteArrayContent)
Expand All @@ -101,7 +97,7 @@ class RegistrationTest {
}

val exception = assertThrows<ServerBindingException> {
runBlockingTest {
runBlocking {
client.use { client.preRegisterNode(publicKey) }
}
}
Expand All @@ -120,7 +116,7 @@ class RegistrationTest {
}

val exception = assertThrows<ClientBindingException> {
runBlockingTest {
runBlocking {
client.use { client.preRegisterNode(publicKey) }
}
}
Expand All @@ -133,7 +129,7 @@ class RegistrationTest {

@Test
fun `Registration request should be output if pre-registration succeeds`() =
runBlockingTest {
runBlocking {
val authorizationSerialized = "This is the PNRA".toByteArray()
val client = makeTestClient {
respond(authorizationSerialized, headers = responseHeaders)
Expand Down Expand Up @@ -161,7 +157,7 @@ class RegistrationTest {
private val registrationSerialized = registration.serialize()

@Test
fun `Request method should be POST`() = runBlockingTest {
fun `Request method should be POST`() = runBlocking {
var method: HttpMethod? = null
val client = makeTestClient { request: HttpRequestData ->
method = request.method
Expand All @@ -174,7 +170,7 @@ class RegistrationTest {
}

@Test
fun `Request should be made to the appropriate endpoint`() = runBlockingTest {
fun `Request should be made to the appropriate endpoint`() = runBlocking {
var endpointURL: String? = null
val client = makeTestClient { request: HttpRequestData ->
endpointURL = request.url.toString()
Expand All @@ -187,7 +183,7 @@ class RegistrationTest {
}

@Test
fun `Request Content-Type should be a PNRR`() = runBlockingTest {
fun `Request Content-Type should be a PNRR`() = runBlocking {
var contentType: ContentType? = null
val client = makeTestClient { request: HttpRequestData ->
contentType = request.body.contentType
Expand All @@ -200,7 +196,7 @@ class RegistrationTest {
}

@Test
fun `Request body should be the PNRR serialized`() = runBlockingTest {
fun `Request body should be the PNRR serialized`() = runBlocking {
var requestBody: ByteArray? = null
val client = makeTestClient { request: HttpRequestData ->
assertTrue(request.body is OutgoingContent.ByteArrayContent)
Expand All @@ -224,7 +220,7 @@ class RegistrationTest {
}

val exception = assertThrows<ServerBindingException> {
runBlockingTest {
runBlocking {
client.use { client.registerNode(pnrrSerialized) }
}
}
Expand All @@ -242,7 +238,7 @@ class RegistrationTest {
}

val exception = assertThrows<ServerBindingException> {
runBlockingTest {
runBlocking {
client.use { client.registerNode(pnrrSerialized) }
}
}
Expand All @@ -252,13 +248,13 @@ class RegistrationTest {
}

@Test
fun `Exception should be thrown if server reports we violated binding`() = runBlockingTest {
fun `Exception should be thrown if server reports we violated binding`() = runBlocking {
val client = makeTestClient {
respond("{}", status = HttpStatusCode.Forbidden)
}

val exception = assertThrows<ClientBindingException> {
runBlockingTest {
runBlocking {
client.use { client.registerNode(pnrrSerialized) }
}
}
Expand All @@ -270,7 +266,7 @@ class RegistrationTest {
}

@Test
fun `Registration should be output if request succeeds`() = runBlockingTest {
fun `Registration should be output if request succeeds`() = runBlocking {
val client = makeTestClient {
respond(registrationSerialized, headers = responseHeaders)
}
Expand Down

0 comments on commit 1cca916

Please sign in to comment.