-
Notifications
You must be signed in to change notification settings - Fork 246
/
SDURLCacheTests.m
145 lines (123 loc) · 10.2 KB
/
SDURLCacheTests.m
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
//
// SDURLCacheTests.m
// SDURLCache
//
// Created by Olivier Poitrey on 16/03/10.
// Copyright 2010 Dailymotion. All rights reserved.
//
#import "SDURLCacheTests.h"
#import "SDURLCache.h"
@interface SDURLCache ()
+ (NSDate *)dateFromHttpDateString:(NSString *)httpDate;
+ (NSDate *)expirationDateFromHeaders:(NSDictionary *)headers withStatusCode:(NSInteger)status;
@end
@implementation SDURLCacheTests
- (void)testHttpDateParser
{
NSDate *date;
NSTimeInterval referenceTime = 784111777;
// RFC 1123 date format
date = [SDURLCache dateFromHttpDateString:@"Sun, 06 Nov 1994 08:49:37 GMT"];
STAssertEquals([date timeIntervalSince1970], referenceTime, @"RFC 1123 date format");
// ANSI C date format
date = [SDURLCache dateFromHttpDateString:@"Sun Nov 6 08:49:37 1994"];
STAssertEquals([date timeIntervalSince1970], referenceTime, @"ANSI C date format %f", [date timeIntervalSince1970]);
// RFC 850 date format
date = [SDURLCache dateFromHttpDateString:@"Sunday, 06-Nov-94 08:49:37 GMT"];
STAssertEquals([date timeIntervalSince1970], referenceTime, @"RFC 850 date format");
}
- (void)testExpirationDateFromHeader
{
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"EEE, dd MMM yyyy HH:mm:ss z"];
NSDate *now = [NSDate date];
NSString *pastDate = [dateFormatter stringFromDate:[NSDate dateWithTimeInterval:-1000 sinceDate:now]];
NSString *nowDate = [dateFormatter stringFromDate:now];
NSString *futureDate = [dateFormatter stringFromDate:[NSDate dateWithTimeInterval:1000 sinceDate:now]];
NSDate *expDate;
// No cache control
expDate = [SDURLCache expirationDateFromHeaders:[NSDictionary dictionaryWithObjectsAndKeys:nowDate, @"Date", nil] withStatusCode:200];
STAssertNotNil(expDate, @"No cache control returns a default expiration date");
STAssertEqualsWithAccuracy([expDate timeIntervalSinceNow], (NSTimeInterval)3600, 1, @"Default expiration date is 1 hour");
// No cache control but last-modified
expDate = [SDURLCache expirationDateFromHeaders:[NSDictionary dictionaryWithObjectsAndKeys:pastDate, @"Last-Modified", nowDate, @"Date", nil] withStatusCode:200];
STAssertNotNil(expDate, @"No cache control with last-modified header returns an expiration date");
STAssertEqualsWithAccuracy([expDate timeIntervalSinceNow], (NSTimeInterval)100, 1, @"Expiration date relative to last-modified is 10%% of the age");
// Pragma: no-cache
expDate = [SDURLCache expirationDateFromHeaders:[NSDictionary dictionaryWithObjectsAndKeys:@"no-cache", @"Pragma", futureDate, @"Expires", nil] withStatusCode:200];
STAssertNil(expDate, @"Pragma no-cache");
// Expires in the past
expDate = [SDURLCache expirationDateFromHeaders:[NSDictionary dictionaryWithObjectsAndKeys:pastDate, @"Expires", nil] withStatusCode:200];
STAssertNil(expDate, @"Expires in the past");
// Expires in the past
expDate = [SDURLCache expirationDateFromHeaders:[NSDictionary dictionaryWithObjectsAndKeys:futureDate, @"Expires", nil] withStatusCode:200];
STAssertTrue([expDate timeIntervalSinceNow] > 0, @"Expires in the future");
// Cache-Control: no-cache with Expires in the future
expDate = [SDURLCache expirationDateFromHeaders:[NSDictionary dictionaryWithObjectsAndKeys:@"no-cache", @"Cache-Control", futureDate, @"Expires", nil] withStatusCode:200];
STAssertTrue([expDate timeIntervalSinceNow] > 0, @"Cache-Control no-cache with Expires in the future");
// Cache-Control with future date
expDate = [SDURLCache expirationDateFromHeaders:[NSDictionary dictionaryWithObjectsAndKeys:@"public, max-age=1000", @"Cache-Control", nil] withStatusCode:200];
STAssertNotNil(expDate, @"Cache-Control with future date");
STAssertTrue([expDate timeIntervalSinceNow] > 0, @"Cache-Control with future date");
// Cache-Control with max-age=0 and Expires future date
expDate = [SDURLCache expirationDateFromHeaders:[NSDictionary dictionaryWithObjectsAndKeys:@"public, max-age=0", @"Cache-Control",
futureDate, @"Expires", nil] withStatusCode:200];
STAssertNil(expDate, @"Cache-Control with max-age=0 and Expires future date");
// Cache-Control with future date and Expires past date
expDate = [SDURLCache expirationDateFromHeaders:[NSDictionary dictionaryWithObjectsAndKeys:@"public, max-age=1000", @"Cache-Control", pastDate, @"Expires", nil] withStatusCode:200];
STAssertNotNil(expDate, @"Cache-Control with future date and Expires past date");
STAssertTrue([expDate timeIntervalSinceNow] > 0, @"Cache-Control with future date and Expires past date");
STAssertNil([SDURLCache expirationDateFromHeaders:nil withStatusCode:100], @"Response status code 100 is not cacheable");
STAssertNil([SDURLCache expirationDateFromHeaders:nil withStatusCode:101], @"Response status code 101 is not cacheable");
STAssertNotNil([SDURLCache expirationDateFromHeaders:nil withStatusCode:200], @"Response status code 200 is cacheable");
STAssertNil([SDURLCache expirationDateFromHeaders:nil withStatusCode:201], @"Response status code 201 is not cacheable");
STAssertNil([SDURLCache expirationDateFromHeaders:nil withStatusCode:202], @"Response status code 202 is not cacheable");
STAssertNotNil([SDURLCache expirationDateFromHeaders:nil withStatusCode:203], @"Response status code 203 is cacheable");
STAssertNil([SDURLCache expirationDateFromHeaders:nil withStatusCode:204], @"Response status code 204 is not cacheable");
STAssertNil([SDURLCache expirationDateFromHeaders:nil withStatusCode:205], @"Response status code 205 is not cacheable");
STAssertNil([SDURLCache expirationDateFromHeaders:nil withStatusCode:206], @"Response status code 206 is not cacheable");
STAssertNotNil([SDURLCache expirationDateFromHeaders:nil withStatusCode:300], @"Response status code 300 is cacheable");
STAssertNotNil([SDURLCache expirationDateFromHeaders:nil withStatusCode:301], @"Response status code 301 is cacheable");
STAssertNil([SDURLCache expirationDateFromHeaders:nil withStatusCode:302], @"Response status code 302 is not cacheable if not explicitly instructed");
expDate = [SDURLCache expirationDateFromHeaders:[NSDictionary dictionaryWithObjectsAndKeys:@"public, max-age=1000", @"Cache-Control", nil] withStatusCode:302];
STAssertNotNil(expDate, @"Response status code 302 is cacheable if explicitly instructed");
STAssertNil([SDURLCache expirationDateFromHeaders:nil withStatusCode:303], @"Response status code 303 is not cacheable");
STAssertNil([SDURLCache expirationDateFromHeaders:nil withStatusCode:304], @"Response status code 304 is not cacheable");
STAssertNil([SDURLCache expirationDateFromHeaders:nil withStatusCode:305], @"Response status code 305 is not cacheable");
STAssertNil([SDURLCache expirationDateFromHeaders:nil withStatusCode:307], @"Response status code 305 is not cacheable if not explicitly instructed");
expDate = [SDURLCache expirationDateFromHeaders:[NSDictionary dictionaryWithObjectsAndKeys:@"public, max-age=1000", @"Cache-Control", nil] withStatusCode:307];
STAssertNotNil(expDate, @"Response status code 307 is cacheable if explicitly instructed");
STAssertNil([SDURLCache expirationDateFromHeaders:nil withStatusCode:400], @"Response status code 400 is not cacheable");
STAssertNil([SDURLCache expirationDateFromHeaders:nil withStatusCode:401], @"Response status code 401 is not cacheable");
STAssertNil([SDURLCache expirationDateFromHeaders:nil withStatusCode:402], @"Response status code 402 is not cacheable");
STAssertNil([SDURLCache expirationDateFromHeaders:nil withStatusCode:403], @"Response status code 403 is not cacheable");
STAssertNil([SDURLCache expirationDateFromHeaders:nil withStatusCode:404], @"Response status code 404 is not cacheable");
STAssertNil([SDURLCache expirationDateFromHeaders:nil withStatusCode:405], @"Response status code 405 is not cacheable");
STAssertNil([SDURLCache expirationDateFromHeaders:nil withStatusCode:406], @"Response status code 406 is not cacheable");
STAssertNil([SDURLCache expirationDateFromHeaders:nil withStatusCode:407], @"Response status code 407 is not cacheable");
STAssertNil([SDURLCache expirationDateFromHeaders:nil withStatusCode:408], @"Response status code 408 is not cacheable");
STAssertNil([SDURLCache expirationDateFromHeaders:nil withStatusCode:409], @"Response status code 409 is not cacheable");
STAssertNotNil([SDURLCache expirationDateFromHeaders:nil withStatusCode:410], @"Response status code 410 is cacheable");
STAssertNil([SDURLCache expirationDateFromHeaders:nil withStatusCode:411], @"Response status code 411 is not cacheable");
STAssertNil([SDURLCache expirationDateFromHeaders:nil withStatusCode:412], @"Response status code 412 is not cacheable");
STAssertNil([SDURLCache expirationDateFromHeaders:nil withStatusCode:413], @"Response status code 413 is not cacheable");
STAssertNil([SDURLCache expirationDateFromHeaders:nil withStatusCode:414], @"Response status code 414 is not cacheable");
STAssertNil([SDURLCache expirationDateFromHeaders:nil withStatusCode:415], @"Response status code 415 is not cacheable");
STAssertNil([SDURLCache expirationDateFromHeaders:nil withStatusCode:416], @"Response status code 416 is not cacheable");
STAssertNil([SDURLCache expirationDateFromHeaders:nil withStatusCode:417], @"Response status code 417 is not cacheable");
STAssertNil([SDURLCache expirationDateFromHeaders:nil withStatusCode:500], @"Response status code 500 is not cacheable");
STAssertNil([SDURLCache expirationDateFromHeaders:nil withStatusCode:501], @"Response status code 501 is not cacheable");
STAssertNil([SDURLCache expirationDateFromHeaders:nil withStatusCode:502], @"Response status code 502 is not cacheable");
STAssertNil([SDURLCache expirationDateFromHeaders:nil withStatusCode:503], @"Response status code 503 is not cacheable");
STAssertNil([SDURLCache expirationDateFromHeaders:nil withStatusCode:504], @"Response status code 504 is not cacheable");
STAssertNil([SDURLCache expirationDateFromHeaders:nil withStatusCode:505], @"Response status code 505 is not cacheable");
}
- (void)testCaching
{
// TODO
}
- (void)testCacheCapacity
{
// TODO
}
@end