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