OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. |
| 4 |
| 5 library shelf.http_date_test; |
| 6 |
| 7 import 'package:shelf/src/util.dart'; |
| 8 import 'package:unittest/unittest.dart'; |
| 9 |
| 10 void main() { |
| 11 group("parse", () { |
| 12 group("RFC 1123", () { |
| 13 test("parses the example date", () { |
| 14 var date = parseHttpDate("Sun, 06 Nov 1994 08:49:37 GMT"); |
| 15 expect(date.day, equals(6)); |
| 16 expect(date.month, equals(DateTime.NOVEMBER)); |
| 17 expect(date.year, equals(1994)); |
| 18 expect(date.hour, equals(8)); |
| 19 expect(date.minute, equals(49)); |
| 20 expect(date.second, equals(37)); |
| 21 expect(date.timeZoneName, equals("UTC")); |
| 22 }); |
| 23 |
| 24 test("whitespace is required", () { |
| 25 expect(() => parseHttpDate("Sun,06 Nov 1994 08:49:37 GMT"), |
| 26 throwsFormatException); |
| 27 |
| 28 expect(() => parseHttpDate("Sun, 06Nov 1994 08:49:37 GMT"), |
| 29 throwsFormatException); |
| 30 |
| 31 expect(() => parseHttpDate("Sun, 06 Nov1994 08:49:37 GMT"), |
| 32 throwsFormatException); |
| 33 |
| 34 expect(() => parseHttpDate("Sun, 06 Nov 199408:49:37 GMT"), |
| 35 throwsFormatException); |
| 36 |
| 37 expect(() => parseHttpDate("Sun, 06 Nov 1994 08:49:37GMT"), |
| 38 throwsFormatException); |
| 39 }); |
| 40 |
| 41 test("exactly one space is required", () { |
| 42 expect(() => parseHttpDate("Sun, 06 Nov 1994 08:49:37 GMT"), |
| 43 throwsFormatException); |
| 44 |
| 45 expect(() => parseHttpDate("Sun, 06 Nov 1994 08:49:37 GMT"), |
| 46 throwsFormatException); |
| 47 |
| 48 expect(() => parseHttpDate("Sun, 06 Nov 1994 08:49:37 GMT"), |
| 49 throwsFormatException); |
| 50 |
| 51 expect(() => parseHttpDate("Sun, 06 Nov 1994 08:49:37 GMT"), |
| 52 throwsFormatException); |
| 53 |
| 54 expect(() => parseHttpDate("Sun, 06 Nov 1994 08:49:37 GMT"), |
| 55 throwsFormatException); |
| 56 }); |
| 57 |
| 58 test("requires precise number lengths", () { |
| 59 expect(() => parseHttpDate("Sun, 6 Nov 1994 08:49:37 GMT"), |
| 60 throwsFormatException); |
| 61 |
| 62 expect(() => parseHttpDate("Sun, 06 Nov 94 08:49:37 GMT"), |
| 63 throwsFormatException); |
| 64 |
| 65 expect(() => parseHttpDate("Sun, 06 Nov 1994 8:49:37 GMT"), |
| 66 throwsFormatException); |
| 67 |
| 68 expect(() => parseHttpDate("Sun, 06 Nov 1994 08:9:37 GMT"), |
| 69 throwsFormatException); |
| 70 |
| 71 expect(() => parseHttpDate("Sun, 06 Nov 1994 08:49:7 GMT"), |
| 72 throwsFormatException); |
| 73 }); |
| 74 |
| 75 test("requires reasonable numbers", () { |
| 76 expect(() => parseHttpDate("Sun, 00 Nov 1994 08:49:37 GMT"), |
| 77 throwsFormatException); |
| 78 |
| 79 expect(() => parseHttpDate("Sun, 31 Nov 1994 08:49:37 GMT"), |
| 80 throwsFormatException); |
| 81 |
| 82 expect(() => parseHttpDate("Sun, 32 Aug 1994 08:49:37 GMT"), |
| 83 throwsFormatException); |
| 84 |
| 85 expect(() => parseHttpDate("Sun, 06 Nov 1994 24:49:37 GMT"), |
| 86 throwsFormatException); |
| 87 |
| 88 expect(() => parseHttpDate("Sun, 06 Nov 1994 08:60:37 GMT"), |
| 89 throwsFormatException); |
| 90 |
| 91 expect(() => parseHttpDate("Sun, 06 Nov 1994 08:49:60 GMT"), |
| 92 throwsFormatException); |
| 93 }); |
| 94 |
| 95 test("only allows short weekday names", () { |
| 96 expect(() => parseHttpDate("Sunday, 6 Nov 1994 08:49:37 GMT"), |
| 97 throwsFormatException); |
| 98 }); |
| 99 |
| 100 test("only allows short month names", () { |
| 101 expect(() => parseHttpDate("Sun, 6 November 1994 08:49:37 GMT"), |
| 102 throwsFormatException); |
| 103 }); |
| 104 |
| 105 test("only allows GMT", () { |
| 106 expect(() => parseHttpDate("Sun, 6 Nov 1994 08:49:37 PST"), |
| 107 throwsFormatException); |
| 108 }); |
| 109 |
| 110 test("disallows trailing whitespace", () { |
| 111 expect(() => parseHttpDate("Sun, 6 Nov 1994 08:49:37 GMT "), |
| 112 throwsFormatException); |
| 113 }); |
| 114 }); |
| 115 |
| 116 group("RFC 850", () { |
| 117 test("parses the example date", () { |
| 118 var date = parseHttpDate("Sunday, 06-Nov-94 08:49:37 GMT"); |
| 119 expect(date.day, equals(6)); |
| 120 expect(date.month, equals(DateTime.NOVEMBER)); |
| 121 expect(date.year, equals(1994)); |
| 122 expect(date.hour, equals(8)); |
| 123 expect(date.minute, equals(49)); |
| 124 expect(date.second, equals(37)); |
| 125 expect(date.timeZoneName, equals("UTC")); |
| 126 }); |
| 127 |
| 128 test("whitespace is required", () { |
| 129 expect(() => parseHttpDate("Sunday,06-Nov-94 08:49:37 GMT"), |
| 130 throwsFormatException); |
| 131 |
| 132 expect(() => parseHttpDate("Sunday, 06-Nov-9408:49:37 GMT"), |
| 133 throwsFormatException); |
| 134 |
| 135 expect(() => parseHttpDate("Sunday, 06-Nov-94 08:49:37GMT"), |
| 136 throwsFormatException); |
| 137 }); |
| 138 |
| 139 test("exactly one space is required", () { |
| 140 expect(() => parseHttpDate("Sunday, 06-Nov-94 08:49:37 GMT"), |
| 141 throwsFormatException); |
| 142 |
| 143 expect(() => parseHttpDate("Sunday, 06-Nov-94 08:49:37 GMT"), |
| 144 throwsFormatException); |
| 145 |
| 146 expect(() => parseHttpDate("Sunday, 06-Nov-94 08:49:37 GMT"), |
| 147 throwsFormatException); |
| 148 }); |
| 149 |
| 150 test("requires precise number lengths", () { |
| 151 expect(() => parseHttpDate("Sunday, 6-Nov-94 08:49:37 GMT"), |
| 152 throwsFormatException); |
| 153 |
| 154 expect(() => parseHttpDate("Sunday, 06-Nov-1994 08:49:37 GMT"), |
| 155 throwsFormatException); |
| 156 |
| 157 expect(() => parseHttpDate("Sunday, 06-Nov-94 8:49:37 GMT"), |
| 158 throwsFormatException); |
| 159 |
| 160 expect(() => parseHttpDate("Sunday, 06-Nov-94 08:9:37 GMT"), |
| 161 throwsFormatException); |
| 162 |
| 163 expect(() => parseHttpDate("Sunday, 06-Nov-94 08:49:7 GMT"), |
| 164 throwsFormatException); |
| 165 }); |
| 166 |
| 167 test("requires reasonable numbers", () { |
| 168 expect(() => parseHttpDate("Sunday, 00-Nov-94 08:49:37 GMT"), |
| 169 throwsFormatException); |
| 170 |
| 171 expect(() => parseHttpDate("Sunday, 31-Nov-94 08:49:37 GMT"), |
| 172 throwsFormatException); |
| 173 |
| 174 expect(() => parseHttpDate("Sunday, 32-Aug-94 08:49:37 GMT"), |
| 175 throwsFormatException); |
| 176 |
| 177 expect(() => parseHttpDate("Sunday, 06-Nov-94 24:49:37 GMT"), |
| 178 throwsFormatException); |
| 179 |
| 180 expect(() => parseHttpDate("Sunday, 06-Nov-94 08:60:37 GMT"), |
| 181 throwsFormatException); |
| 182 |
| 183 expect(() => parseHttpDate("Sunday, 06-Nov-94 08:49:60 GMT"), |
| 184 throwsFormatException); |
| 185 }); |
| 186 |
| 187 test("only allows long weekday names", () { |
| 188 expect(() => parseHttpDate("Sun, 6-Nov-94 08:49:37 GMT"), |
| 189 throwsFormatException); |
| 190 }); |
| 191 |
| 192 test("only allows short month names", () { |
| 193 expect(() => parseHttpDate("Sunday, 6-November-94 08:49:37 GMT"), |
| 194 throwsFormatException); |
| 195 }); |
| 196 |
| 197 test("only allows GMT", () { |
| 198 expect(() => parseHttpDate("Sunday, 6-Nov-94 08:49:37 PST"), |
| 199 throwsFormatException); |
| 200 }); |
| 201 |
| 202 test("disallows trailing whitespace", () { |
| 203 expect(() => parseHttpDate("Sunday, 6-Nov-94 08:49:37 GMT "), |
| 204 throwsFormatException); |
| 205 }); |
| 206 }); |
| 207 |
| 208 group("asctime()", () { |
| 209 test("parses the example date", () { |
| 210 var date = parseHttpDate("Sun Nov 6 08:49:37 1994"); |
| 211 expect(date.day, equals(6)); |
| 212 expect(date.month, equals(DateTime.NOVEMBER)); |
| 213 expect(date.year, equals(1994)); |
| 214 expect(date.hour, equals(8)); |
| 215 expect(date.minute, equals(49)); |
| 216 expect(date.second, equals(37)); |
| 217 expect(date.timeZoneName, equals("UTC")); |
| 218 }); |
| 219 |
| 220 test("parses a date with a two-digit day", () { |
| 221 var date = parseHttpDate("Sun Nov 16 08:49:37 1994"); |
| 222 expect(date.day, equals(16)); |
| 223 expect(date.month, equals(DateTime.NOVEMBER)); |
| 224 expect(date.year, equals(1994)); |
| 225 expect(date.hour, equals(8)); |
| 226 expect(date.minute, equals(49)); |
| 227 expect(date.second, equals(37)); |
| 228 expect(date.timeZoneName, equals("UTC")); |
| 229 }); |
| 230 |
| 231 test("whitespace is required", () { |
| 232 expect(() => parseHttpDate("SunNov 6 08:49:37 1994"), |
| 233 throwsFormatException); |
| 234 |
| 235 expect(() => parseHttpDate("Sun Nov6 08:49:37 1994"), |
| 236 throwsFormatException); |
| 237 |
| 238 expect(() => parseHttpDate("Sun Nov 608:49:37 1994"), |
| 239 throwsFormatException); |
| 240 |
| 241 expect(() => parseHttpDate("Sun Nov 6 08:49:371994"), |
| 242 throwsFormatException); |
| 243 }); |
| 244 |
| 245 test("the right amount of whitespace is required", () { |
| 246 expect(() => parseHttpDate("Sun Nov 6 08:49:37 1994"), |
| 247 throwsFormatException); |
| 248 |
| 249 expect(() => parseHttpDate("Sun Nov 6 08:49:37 1994"), |
| 250 throwsFormatException); |
| 251 |
| 252 expect(() => parseHttpDate("Sun Nov 6 08:49:37 1994"), |
| 253 throwsFormatException); |
| 254 |
| 255 expect(() => parseHttpDate("Sun Nov 6 08:49:37 1994"), |
| 256 throwsFormatException); |
| 257 |
| 258 expect(() => parseHttpDate("Sun Nov 6 08:49:37 1994"), |
| 259 throwsFormatException); |
| 260 }); |
| 261 |
| 262 test("requires precise number lengths", () { |
| 263 expect(() => parseHttpDate("Sun Nov 016 08:49:37 1994"), |
| 264 throwsFormatException); |
| 265 |
| 266 expect(() => parseHttpDate("Sun Nov 6 8:49:37 1994"), |
| 267 throwsFormatException); |
| 268 |
| 269 expect(() => parseHttpDate("Sun Nov 6 08:9:37 1994"), |
| 270 throwsFormatException); |
| 271 |
| 272 expect(() => parseHttpDate("Sun Nov 6 08:49:7 1994"), |
| 273 throwsFormatException); |
| 274 |
| 275 expect(() => parseHttpDate("Sun Nov 6 08:49:37 94"), |
| 276 throwsFormatException); |
| 277 }); |
| 278 |
| 279 test("requires reasonable numbers", () { |
| 280 expect(() => parseHttpDate("Sun Nov 0 08:49:37 1994"), |
| 281 throwsFormatException); |
| 282 |
| 283 expect(() => parseHttpDate("Sun Nov 31 08:49:37 1994"), |
| 284 throwsFormatException); |
| 285 |
| 286 expect(() => parseHttpDate("Sun Aug 32 08:49:37 1994"), |
| 287 throwsFormatException); |
| 288 |
| 289 expect(() => parseHttpDate("Sun Nov 6 24:49:37 1994"), |
| 290 throwsFormatException); |
| 291 |
| 292 expect(() => parseHttpDate("Sun Nov 6 08:60:37 1994"), |
| 293 throwsFormatException); |
| 294 |
| 295 expect(() => parseHttpDate("Sun Nov 6 08:49:60 1994"), |
| 296 throwsFormatException); |
| 297 }); |
| 298 |
| 299 test("only allows short weekday names", () { |
| 300 expect(() => parseHttpDate("Sunday Nov 0 08:49:37 1994"), |
| 301 throwsFormatException); |
| 302 }); |
| 303 |
| 304 test("only allows short month names", () { |
| 305 expect(() => parseHttpDate("Sun November 0 08:49:37 1994"), |
| 306 throwsFormatException); |
| 307 }); |
| 308 |
| 309 test("disallows trailing whitespace", () { |
| 310 expect(() => parseHttpDate("Sun November 0 08:49:37 1994 "), |
| 311 throwsFormatException); |
| 312 }); |
| 313 }); |
| 314 }); |
| 315 } |
OLD | NEW |