-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsmithy4s-fetch.test.scala
227 lines (192 loc) · 6.24 KB
/
smithy4s-fetch.test.scala
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
//> using test.dep com.disneystreaming::weaver-cats::0.8.4
//> using test.dep "tech.neander::smithy4s-deriving::0.0.2"
//> using test.dep com.disneystreaming.smithy4s::smithy4s-http4s::0.18.22
//> using test.dep org.http4s::http4s-ember-server::0.23.27
//> using test.dep org.http4s::http4s-ember-client::0.23.27
//> using testFramework "weaver.framework.CatsEffect"
//> using scala 3.4.2
package smithy4s_fetch.tests
import cats.effect.IO
import cats.effect.kernel.Resource
import cats.syntax.all.*
import com.comcast.ip4s.port
import org.http4s.Uri
import org.http4s.ember.client.EmberClientBuilder
import org.http4s.ember.server.EmberServerBuilder
import smithy4s.deriving.API
import smithy4s.http.HttpUriScheme
import smithy4s.http4s.SimpleRestJsonBuilder
import smithy4s_fetch.SimpleRestJsonFetchClient
import weaver.{FunSuiteIO, IOSuite}
import scala.concurrent.duration.*
import scala.scalajs.js.Promise
import smithy4s.http.HttpUri
import org.scalajs.dom.URL
object UnitTest extends FunSuiteIO:
val uri =
smithy4s.http.HttpUri(
scheme = HttpUriScheme.Https,
path = Vector("hello", "world"),
queryParams = Map(
"k" -> Seq.empty,
"k2" -> Seq("hello"),
"k3" -> Seq("hello", "world", "!")
),
host = "localhost",
pathParams = None,
port = Some(9999)
)
def enc(uri: HttpUri): String =
smithy4s_fetch.SimpleRestJsonCodecs.fromSmithy4sHttpUri(uri)
test("URI encoding"):
expect.same(
enc(uri),
"https://localhost:9999/hello/world?k&k2=hello&k3=hello&k3=world&k3=!"
) &&
expect.same(
enc(uri.copy(queryParams = Map.empty)),
"https://localhost:9999/hello/world"
) &&
expect.same(
enc(uri.copy(queryParams = Map.empty, scheme = HttpUriScheme.Http)),
"http://localhost:9999/hello/world"
) &&
expect.same(
enc(uri.copy(queryParams = Map.empty, host = "hello.com")),
"https://hello.com:9999/hello/world"
) &&
expect.same(
enc(uri.copy(queryParams = Map.empty, port = None)),
"https://localhost/hello/world"
) &&
expect.same(
enc(uri.copy(queryParams = Map.empty, path = Vector.empty)),
"https://localhost:9999/"
) &&
expect.same(
enc(uri.copy(queryParams = Map.empty, path = Vector("1", "2", "3"))),
"https://localhost:9999/1/2/3"
)
test("Base URI with no path prefix"):
val result = smithy4s_fetch.SimpleRestJsonCodecs
.toSmithy4sHttpUri(new URL("http://localhost"))
.path
expect(result.isEmpty)
test("Base URI with no path prefix (with slash)"):
val result = smithy4s_fetch.SimpleRestJsonCodecs
.toSmithy4sHttpUri(new URL("http://localhost/"))
.path
expect(result.isEmpty)
test("Base URI with path prefix"):
expect.same(
smithy4s_fetch.SimpleRestJsonCodecs
.toSmithy4sHttpUri(new URL("http://localhost/prefix"))
.path,
IndexedSeq("prefix")
)
test("Base URI with no path prefix, including empty segments"):
expect.same(
smithy4s_fetch.SimpleRestJsonCodecs
.toSmithy4sHttpUri(new URL("http://localhost/foo//bar//baz/"))
.path,
IndexedSeq("foo", "", "bar", "", "baz")
)
test("Base URI with path prefix, trailing slash doesn't matter"):
expect.same(
smithy4s_fetch.SimpleRestJsonCodecs
.toSmithy4sHttpUri(new URL("http://localhost/foo/")),
smithy4s_fetch.SimpleRestJsonCodecs
.toSmithy4sHttpUri(new URL("http://localhost/foo"))
)
@annotation.experimental
object IntegrationTest extends IOSuite:
val service = API.service[IOService]
val promiseService = API.service[PromiseService]
val routesResource =
SimpleRestJsonBuilder
.routes(IOService().liftService[IO])
.resource
.map(_.orNotFound)
case class Probe(
serverUri: Uri,
ioClient: IOService,
fetchClient: PromiseService
)
override type Res = Probe
override def sharedResource: Resource[IO, Res] =
val serverUri = routesResource.flatMap: app =>
EmberServerBuilder
.default[IO]
.withPort(port"0")
.withHttpApp(app)
.withShutdownTimeout(0.seconds)
.build
.map(_.baseUri)
serverUri.flatMap: uri =>
val http4sClient = EmberClientBuilder
.default[IO]
.build
.flatMap: httpClient =>
SimpleRestJsonBuilder(service)
.client[IO](httpClient)
.uri(uri)
.resource
.map(_.unliftService)
val fetchClient =
IO.pure(
SimpleRestJsonFetchClient(
promiseService,
uri.renderString
).make.unliftService
).toResource
(http4sClient, fetchClient).mapN((io, fetch) => Probe(uri, io, fetch))
end sharedResource
test("hello response"): res =>
for
ioResp <- res.ioClient.hello()
fetchResp <- IO.fromFuture(IO(res.fetchClient.hello().toFuture))
yield expect.same(ioResp, fetchResp)
test("stub response"): res =>
for
ioResp <- res.ioClient.stub(IP("yo"), "bruh")
fetchResp <- IO.fromFuture(
IO(res.fetchClient.stub(IP("yo"), "bruh").toFuture)
)
yield expect.same(ioResp, fetchResp)
end IntegrationTest
import smithy4s.*, deriving.{given, *}, aliases.*
import scala.annotation.experimental // the derivation of API uses experimental metaprogramming features, at this time.
trait Routes[F[_]]:
def hello(): F[IP]
def stub(ip: IP, name: String): F[StubResponse]
@experimental
@simpleRestJson
class IOService() extends Routes[IO] derives API:
@readonly
@httpGet("/httpbin/ip")
override def hello(): IO[IP] = IO.pure(IP("127.0.0.1"))
@httpDelete("/httpbin/delete")
override def stub(
ip: IP,
@httpQuery("username") name: String
): IO[StubResponse] =
IO.pure(
StubResponse(s"hello, $name", "http://localhost", Map.empty, myIP = ip)
)
@experimental
trait PromiseService extends Routes[Promise] derives API:
@readonly
@httpGet("/httpbin/ip")
override def hello(): Promise[IP]
@httpDelete("/httpbin/delete")
override def stub(
ip: IP,
@httpQuery("username") name: String
): Promise[StubResponse]
case class IP(origin: String) derives Schema
case class StubResponse(
origin: String,
url: String,
headers: Map[String, String],
myIP: IP
) derives Schema