-
-
Notifications
You must be signed in to change notification settings - Fork 353
/
ZnStaticFileServerDelegateTest.class.st
173 lines (161 loc) · 5.6 KB
/
ZnStaticFileServerDelegateTest.class.st
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
Class {
#name : #ZnStaticFileServerDelegateTest,
#superclass : #TestCase,
#category : #'Zinc-Tests'
}
{ #category : #private }
ZnStaticFileServerDelegateTest >> largeHtml [
^ String streamContents: [ :out |
out << '<html><body>'.
out << 'Les élèves Français'.
(1 to: 4000) do: [ :each | out print: each ] separatedBy: [ out space ].
out << '</html></body>' ]
]
{ #category : #running }
ZnStaticFileServerDelegateTest >> setUp [
super setUp.
ZnFileSystemUtils
newFileNamed: 'small.html'
do: [ :stream |
stream nextPutAll: self smallHtml ].
ZnFileSystemUtils
newFileNamed: 'large.html'
do: [ :stream |
stream nextPutAll: self largeHtml ].
ZnFileSystemUtils
newFileNamed: 'wide.html'
do: [ :stream |
stream nextPutAll: self wideHtml ].
]
{ #category : #private }
ZnStaticFileServerDelegateTest >> smallHtml [
^ '<html>
<head><title>Small</title></head>
<body><h1>Small</h1><p>This is a small HTML document</p></body>
</html>
'
]
{ #category : #running }
ZnStaticFileServerDelegateTest >> tearDown [
ZnFileSystemUtils
deleteIfExists: 'small.html';
deleteIfExists: 'large.html';
deleteIfExists: 'wide.html'.
super tearDown
]
{ #category : #tests }
ZnStaticFileServerDelegateTest >> testBasicGet [
self withServerDo: [ :server | | client |
(client := ZnClient new)
beOneShot;
url: server localUrl;
addPath: #('local-files' 'small.html');
get.
self assert: client isSuccess.
self assert: client response contentType = ZnMimeType textHtml.
self assert: client contents equals: self smallHtml.
self
assert: (ZnUtils parseHttpDate: (client response headers at: 'Modification-Date'))
equals: (ZnFileSystemUtils modificationTimeFor: 'small.html') asUTC.
self
assert: (ZnUtils parseHttpDate: (client response headers at: 'Expires')) > (DateAndTime now + 10 days).
self
assert: (client response headers at: 'Cache-Control')
equals: (server delegate maxAgeFor: ZnMimeType textHtml) ]
]
{ #category : #tests }
ZnStaticFileServerDelegateTest >> testBasicGetLarge [
self withServerDo: [ :server | | client |
(client := ZnClient new)
beOneShot;
url: server localUrl;
addPath: #('local-files' 'large.html');
get.
self assert: client isSuccess.
self assert: client response contentType = ZnMimeType textHtml.
self assert: client contents equals: self largeHtml.
self
assert: (ZnUtils parseHttpDate: (client response headers at: 'Modification-Date'))
equals: (ZnFileSystemUtils modificationTimeFor: 'large.html') asUTC.
self
assert: (ZnUtils parseHttpDate: (client response headers at: 'Expires')) > (DateAndTime now + 10 days).
self
assert: (client response headers at: 'Cache-Control')
equals: (server delegate maxAgeFor: ZnMimeType textHtml) ]
]
{ #category : #tests }
ZnStaticFileServerDelegateTest >> testBasicGetWide [
self withServerDo: [ :server | | client |
(client := ZnClient new)
beOneShot;
url: server localUrl;
addPath: #('local-files' 'wide.html');
get.
self assert: client isSuccess.
self assert: client response contentType = ZnMimeType textHtml.
self assert: client contents equals: self wideHtml.
self
assert: (ZnUtils parseHttpDate: (client response headers at: 'Modification-Date'))
equals: (ZnFileSystemUtils modificationTimeFor: 'large.html') asUTC.
self
assert: (ZnUtils parseHttpDate: (client response headers at: 'Expires')) > (DateAndTime now + 10 days).
self
assert: (client response headers at: 'Cache-Control')
equals: (server delegate maxAgeFor: ZnMimeType textHtml) ]
]
{ #category : #tests }
ZnStaticFileServerDelegateTest >> testBasicHead [
self withServerDo: [ :server | | client |
(client := ZnClient new)
beOneShot;
url: server localUrl;
addPath: #('local-files' 'small.html');
head.
self assert: client isSuccess.
self assert: client response contentType = ZnMimeType textHtml.
self deny: client response hasEntity.
self
assert: (ZnUtils parseHttpDate: (client response headers at: 'Modification-Date'))
equals: (ZnFileSystemUtils modificationTimeFor: 'small.html') asUTC.
self
assert: (ZnUtils parseHttpDate: (client response headers at: 'Expires')) > (DateAndTime now + 10 days).
self
assert: (client response headers at: 'Cache-Control')
equals: (server delegate maxAgeFor: ZnMimeType textHtml) ]
]
{ #category : #tests }
ZnStaticFileServerDelegateTest >> testIfModifiedSinceNotModified [
self withServerDo: [ :server | | client |
(client := ZnClient new)
beOneShot;
url: server localUrl;
addPath: #('local-files' 'small.html');
"We have to cheat a little bit since our #setUp just created the file,
it does not alter the semantics of what we try to test"
setIfModifiedSince: (Date today addDays: 2);
get.
self assert: client response isNotModified.
self deny: client response hasEntity ]
]
{ #category : #private }
ZnStaticFileServerDelegateTest >> wideHtml [
^ '<html><h1>Czech is in Czech {1}e{2}tina.</h1></html>' format: { 269 asCharacter. 353 asCharacter}.
]
{ #category : #private }
ZnStaticFileServerDelegateTest >> withServerDo: block [
| server staticFileServerDelegate |
server := ZnServer on: 1700 + 32 atRandom.
[
(staticFileServerDelegate := ZnStaticFileServerDelegate new)
prefixFromString: 'local-files';
directory: ZnFileSystemUtils defaultDirectory;
mimeTypeExpirations: ZnStaticFileServerDelegate defaultMimeTypeExpirations.
server delegate: staticFileServerDelegate.
server start.
self
assert: server isRunning & server isListening
description: ('Failed to start server on port {1}. Is there one already?' format: { server port }).
block cull: server
]
ensure: [ server stop ]
]