@@ -12,135 +12,132 @@ import io.ktor.server.plugins.conditionalheaders.*
12
12
import io.ktor.server.response.*
13
13
import io.ktor.server.routing.*
14
14
import io.ktor.server.testing.*
15
- import io.ktor.util.*
16
15
import kotlin.test.*
17
16
18
- @Suppress(" DEPRECATION" )
19
17
class WebjarsTest {
20
18
21
19
@Test
22
20
fun resourceNotFound () {
23
- withTestApplication {
24
- application. install(Webjars )
25
- handleRequest( HttpMethod . Get , " /webjars/foo.js" ).let { call ->
21
+ testApplication {
22
+ install(Webjars )
23
+ client.get( " /webjars/foo.js" ).let { response ->
26
24
// Should be handled by some other routing
27
- assertEquals(HttpStatusCode .NotFound , call. response.status() )
25
+ assertEquals(HttpStatusCode .NotFound , response.status)
28
26
}
29
27
}
30
28
}
31
29
32
30
@Test
33
31
fun pathLike () {
34
- withTestApplication {
35
- application. install(Webjars )
36
- application. routing {
32
+ testApplication {
33
+ install(Webjars )
34
+ routing {
37
35
get(" /webjars-something/jquery" ) {
38
36
call.respondText { " Something Else" }
39
37
}
40
38
}
41
- handleRequest( HttpMethod . Get , " /webjars-something/jquery" ).let { call ->
42
- assertEquals(HttpStatusCode .OK , call. response.status() )
43
- assertEquals(" Something Else" , call. response.content )
39
+ client.get( " /webjars-something/jquery" ).let { response ->
40
+ assertEquals(HttpStatusCode .OK , response.status)
41
+ assertEquals(" Something Else" , response.bodyAsText() )
44
42
}
45
43
}
46
44
}
47
45
48
46
@Test
49
47
fun nestedPath () {
50
- withTestApplication {
51
- application. install(Webjars ) {
48
+ testApplication {
49
+ install(Webjars ) {
52
50
path = " /assets/webjars"
53
51
}
54
- handleRequest( HttpMethod . Get , " /assets/webjars/jquery/jquery.js" ).let { call ->
55
- assertEquals(HttpStatusCode .OK , call. response.status() )
56
- assertEquals(" application/javascript " , call. response.headers[ " Content-Type " ] )
52
+ client.get( " /assets/webjars/jquery/jquery.js" ).let { response ->
53
+ assertEquals(HttpStatusCode .OK , response.status)
54
+ assertEquals(ContentType . Text . JavaScript , response.contentType()?.withoutParameters() )
57
55
}
58
56
}
59
57
}
60
58
61
59
@Test
62
60
fun rootPath () {
63
- withTestApplication {
64
- application. install(Webjars ) {
61
+ testApplication {
62
+ install(Webjars ) {
65
63
path = " /"
66
64
}
67
- handleRequest( HttpMethod . Get , " /jquery/jquery.js" ).let { call ->
68
- assertEquals(HttpStatusCode .OK , call. response.status() )
69
- assertEquals(" application/javascript " , call. response.headers[ " Content-Type " ] )
65
+ client.get( " /jquery/jquery.js" ).let { response ->
66
+ assertEquals(HttpStatusCode .OK , response.status)
67
+ assertEquals(ContentType . Text . JavaScript , response.contentType()?.withoutParameters() )
70
68
}
71
69
}
72
70
}
73
71
74
72
@Test
75
73
fun rootPath2 () {
76
- withTestApplication {
77
- application. install(Webjars ) {
74
+ testApplication {
75
+ install(Webjars ) {
78
76
path = " /"
79
77
}
80
- application. routing {
78
+ routing {
81
79
get(" /" ) { call.respondText(" Hello, World" ) }
82
80
}
83
- handleRequest( HttpMethod . Get , " /" ).let { call ->
84
- assertEquals(HttpStatusCode .OK , call. response.status() )
85
- assertEquals(" Hello, World" , call. response.content )
81
+ client.get( " /" ).let { response ->
82
+ assertEquals(HttpStatusCode .OK , response.status)
83
+ assertEquals(" Hello, World" , response.bodyAsText() )
86
84
}
87
- handleRequest( HttpMethod . Get , " /jquery/jquery.js" ).let { call ->
88
- assertEquals(HttpStatusCode .OK , call. response.status() )
89
- assertEquals(" application/javascript " , call. response.headers[ " Content-Type " ] )
85
+ client.get( " /jquery/jquery.js" ).let { response ->
86
+ assertEquals(HttpStatusCode .OK , response.status)
87
+ assertEquals(ContentType . Text . JavaScript , response.contentType()?.withoutParameters() )
90
88
}
91
89
}
92
90
}
93
91
94
92
@Test
95
93
fun versionAgnostic () {
96
- withTestApplication {
97
- application. install(Webjars )
94
+ testApplication {
95
+ install(Webjars )
98
96
99
- handleRequest( HttpMethod . Get , " /webjars/jquery/jquery.js" ).let { call ->
100
- assertEquals(HttpStatusCode .OK , call. response.status() )
101
- assertEquals(" application/javascript " , call. response.headers[ " Content-Type " ] )
97
+ client.get( " /webjars/jquery/jquery.js" ).let { response ->
98
+ assertEquals(HttpStatusCode .OK , response.status)
99
+ assertEquals(ContentType . Text . JavaScript , response.contentType()?.withoutParameters() )
102
100
}
103
101
}
104
102
}
105
103
106
104
@Test
107
105
fun withGetParameters () {
108
- withTestApplication {
109
- application. install(Webjars )
106
+ testApplication {
107
+ install(Webjars )
110
108
111
- handleRequest( HttpMethod . Get , " /webjars/jquery/jquery.js?param1=value1" ).let { call ->
112
- assertEquals(HttpStatusCode .OK , call. response.status() )
113
- assertEquals(" application/javascript " , call. response.headers[ " Content-Type " ] )
109
+ client.get( " /webjars/jquery/jquery.js?param1=value1" ).let { response ->
110
+ assertEquals(HttpStatusCode .OK , response.status)
111
+ assertEquals(ContentType . Text . JavaScript , response.contentType()?.withoutParameters() )
114
112
}
115
113
}
116
114
}
117
115
118
116
@Test
119
117
fun withSpecificVersion () {
120
- withTestApplication {
121
- application. install(Webjars )
118
+ testApplication {
119
+ install(Webjars )
122
120
123
- handleRequest( HttpMethod . Get , " /webjars/jquery/3.6.4/jquery.js" ).let { call ->
124
- assertEquals(HttpStatusCode .OK , call. response.status() )
125
- assertEquals(" application/javascript " , call. response.headers[ " Content-Type " ] )
121
+ client.get( " /webjars/jquery/3.6.4/jquery.js" ).let { response ->
122
+ assertEquals(HttpStatusCode .OK , response.status)
123
+ assertEquals(ContentType . Text . JavaScript , response.contentType()?.withoutParameters() )
126
124
}
127
125
}
128
126
}
129
127
130
128
@Test
131
- fun withConditionalHeaders () {
132
- withTestApplication {
133
- application. install(Webjars )
134
- application. install(ConditionalHeaders )
135
- handleRequest( HttpMethod . Get , " /webjars/jquery/3.6.4/jquery.js" ).let { call ->
136
- assertEquals(HttpStatusCode .OK , call. response.status() )
137
- assertEquals(" application/javascript " , call. response.headers[ " Content-Type " ] )
138
- assertNotNull(call. response.headers[" Last-Modified" ])
129
+ fun withConditionalAndCachingHeaders () {
130
+ testApplication {
131
+ install(Webjars )
132
+ install(ConditionalHeaders )
133
+ client.get( " /webjars/jquery/3.6.4/jquery.js" ).let { response ->
134
+ assertEquals(HttpStatusCode .OK , response.status)
135
+ assertEquals(ContentType . Text . JavaScript , response.contentType()?.withoutParameters() )
136
+ assertNotNull(response.headers[" Last-Modified" ])
139
137
}
140
138
}
141
139
}
142
140
143
- @OptIn(InternalAPI ::class )
144
141
@Test
145
142
fun callHandledBeforeWebjars () {
146
143
val alwaysRespondHello = object : Hook <Unit > {
@@ -161,7 +158,7 @@ class WebjarsTest {
161
158
val response = client.get(" /webjars/jquery/3.3.1/jquery.js" )
162
159
assertEquals(HttpStatusCode .OK , response.status)
163
160
assertEquals(" Hello" , response.bodyAsText())
164
- assertNotEquals(" application/javascript " , response.headers[ " Content-Type " ] )
161
+ assertNotEquals(ContentType . Text . JavaScript , response.contentType()?.withoutParameters() )
165
162
}
166
163
}
167
164
}
0 commit comments