Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ internal fun ApolloRequest<*>.toJson() =
*/
fun WebSocketNetworkTransport.Builder.appSync(endpoint: AppSyncEndpoint, authorizer: AppSyncAuthorizer) = apply {
// Set the connection URL
serverUrl(endpoint.websocketConnection.toString())
serverUrl(endpoint.realtime.toString())

// Add User-agent header
addHeader(UserAgentHeader.NAME, UserAgentHeader.value)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class AppSyncEndpoint(serverUrl: String) {
}

// See SubscriptionEndpoint.buildConnectionRequestUrl
private val realtime by lazy {
internal val realtime by lazy {
if (standardEndpointRegex.matches(urlString)) {
// For standard URLs we insert "realtime" into the domain
URL(urlString.replace("appsync-api", "appsync-realtime-api"))
Expand All @@ -53,11 +53,6 @@ class AppSyncEndpoint(serverUrl: String) {
}
}

internal val websocketConnection by lazy {
// See SubscriptionEndpoint.buildConnectionRequestUrl
URL("$realtime/connect")
}

/**
* Creates the serverUrl to be used for the WebSocketTransport's serverUrl. For AppSync, this URL has authorization
* information appended in query parameters. Set this value as the serverUrl for the WebSocketTransport.
Expand All @@ -67,7 +62,7 @@ class AppSyncEndpoint(serverUrl: String) {
val headers = mapOf("host" to serverUrl.host) + authorizer.getWebsocketConnectionHeaders(this)
val authorization = headers.base64()

val url = websocketConnection.toHttpUrlOrNull() ?: error("Invalid endpoint url")
val url = realtime.toHttpUrlOrNull() ?: error("Invalid endpoint url")

return url.newBuilder()
.addQueryParameter("header", authorization)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ class IamAuthorizer(private val generateSignatureHeaders: suspend (HttpRequest)
// See SubscriptionAuthorizer.forIam
override suspend fun getWebsocketConnectionHeaders(endpoint: AppSyncEndpoint): Map<String, String> {
val request = createHttpRequestForSigning(
url = endpoint.websocketConnection.toString(),
url = endpoint.realtime.toString() + "/connect",
host = endpoint.serverUrl.host,
json = "{}"
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ class ApolloExtensionsTest {

verify {
transportBuilder.serverUrl(
"https://example1234567890123456789.appsync-realtime-api.us-east-1.amazonaws.com/graphql/connect"
"https://example1234567890123456789.appsync-realtime-api.us-east-1.amazonaws.com/graphql"
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,21 +46,21 @@ class AppSyncEndpointTest {
@Test
fun `uses expected realtime URL for standard endpoint`() {
val endpoint = AppSyncEndpoint(standardAppSyncUrl)
endpoint.websocketConnection.toString() shouldBe
"https://example1234567890123456789.appsync-realtime-api.us-east-1.amazonaws.com/graphql/connect"
endpoint.realtime.toString() shouldBe
"https://example1234567890123456789.appsync-realtime-api.us-east-1.amazonaws.com/graphql"
}

@Test
fun `uses expected realtime URL for standard endpoint in China`() {
val endpoint = AppSyncEndpoint(standardAppSyncUrlChina)
endpoint.websocketConnection.toString() shouldBe
"https://example1234567890123456789.appsync-realtime-api.us-east-1.amazonaws.com.cn/graphql/connect"
endpoint.realtime.toString() shouldBe
"https://example1234567890123456789.appsync-realtime-api.us-east-1.amazonaws.com.cn/graphql"
}

@Test
fun `uses expected realtime URL for custom endpoint`() {
val endpoint = AppSyncEndpoint(customAppSyncUrl)
endpoint.websocketConnection.toString() shouldBe "https://api.example.com/graphql/realtime/connect"
endpoint.realtime.toString() shouldBe "https://api.example.com/graphql/realtime"
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,11 @@ import com.amplifyframework.apollo.appsync.toJson
import com.apollographql.apollo.api.ApolloRequest
import com.apollographql.apollo.api.http.HttpRequest
import io.kotest.matchers.maps.shouldContainAll
import io.kotest.matchers.shouldBe
import io.mockk.every
import io.mockk.mockk
import io.mockk.mockkStatic
import io.mockk.verify
import kotlinx.coroutines.test.runTest
import org.junit.Test

Expand All @@ -34,6 +36,21 @@ class IamAuthorizerTest {
HttpRequest
) -> Map<String, String> = { mapOf("header1" to "header1Value", "header2" to "header2Value") }

@Test
fun `appends connect to realtime url`() = runTest {
val mockDelegate: (HttpRequest) -> Map<String, String> = mockk(relaxed = true)
val authorizer = IamAuthorizer(generateSignatureHeaders = mockDelegate)
authorizer.getWebsocketConnectionHeaders(endpoint)
verify {
mockDelegate(
withArg {
it.url shouldBe
"https://example1234567890123456789.appsync-realtime-api.us-east-1.amazonaws.com/graphql/connect"
}
)
}
}

@Test
fun `returns authorization header for HTTP requests`() = runTest {
val authorizer = IamAuthorizer(delegate)
Expand Down