| 1 |
#!/usr/bin/env python3 |
| 2 |
from sys import exit |
| 3 |
from test.http_test import HTTPTest |
| 4 |
from misc.wget_file import WgetFile |
| 5 |
|
| 6 |
""" |
| 7 |
Simple test for HTTP Conditional-GET Requests using the -N command |
| 8 |
""" |
| 9 |
############# File Definitions ############################################### |
| 10 |
# Keep same length ! |
| 11 |
Cont1 = """THIS IS 1 FILE""" |
| 12 |
Cont2 = """THIS IS 2 FILE""" |
| 13 |
Cont3 = """THIS IS 3 FILE""" |
| 14 |
Cont4 = """THIS IS 4 FILE""" |
| 15 |
|
| 16 |
# Local Wget files |
| 17 |
|
| 18 |
# These have same timestamp as remote files |
| 19 |
UpToDate_Local_File1 = WgetFile ("UpToDateFile1", Cont1, timestamp="1995-01-01 00:00:00") |
| 20 |
UpToDate_Local_File2 = WgetFile ("UpToDateFile2", Cont1, timestamp="1995-01-01 00:00:00") |
| 21 |
UpToDate_Local_File3 = WgetFile ("UpToDateFile3", Cont1, timestamp="1995-01-01 00:00:00") |
| 22 |
|
| 23 |
# This is newer than remote (expected same behaviour as for above files) |
| 24 |
Newer_Local_File = WgetFile ("NewerFile", Cont1, timestamp="1995-02-02 02:02:02") |
| 25 |
|
| 26 |
# This is older than remote - should be clobbered |
| 27 |
Outdated_Local_File = WgetFile ("UpdatedFile", Cont2, timestamp="1990-01-01 00:00:00") |
| 28 |
|
| 29 |
UpToDate_Rules1 = { |
| 30 |
"SendHeader" : { |
| 31 |
# RFC1123 format |
| 32 |
"Last-Modified" : "Sun, 01 Jan 1995 00:00:00 GMT", |
| 33 |
}, |
| 34 |
"Response": 304, |
| 35 |
"ExpectHeader" : { |
| 36 |
"If-Modified-Since" : "Sun, 01 Jan 1995 00:00:00 GMT" |
| 37 |
}, |
| 38 |
} |
| 39 |
|
| 40 |
UpToDate_Rules2 = { |
| 41 |
"SendHeader" : { |
| 42 |
# RFC850 format |
| 43 |
"Last-Modified" : "Sunday, 01-Jan-95 00:00:00 GMT", |
| 44 |
}, |
| 45 |
"Response": 304, |
| 46 |
"ExpectHeader" : { |
| 47 |
"If-Modified-Since" : "Sun, 01 Jan 1995 00:00:00 GMT" |
| 48 |
}, |
| 49 |
} |
| 50 |
|
| 51 |
UpToDate_Rules3 = { |
| 52 |
"SendHeader" : { |
| 53 |
# Asctime format |
| 54 |
"Last-Modified" : "Sun Jan 01 00:00:00 1995", |
| 55 |
}, |
| 56 |
"Response": 304, |
| 57 |
"ExpectHeader" : { |
| 58 |
"If-Modified-Since" : "Sun, 01 Jan 1995 00:00:00 GMT" |
| 59 |
}, |
| 60 |
} |
| 61 |
|
| 62 |
Newer_Rules = { |
| 63 |
"SendHeader" : { |
| 64 |
# Asctime format |
| 65 |
"Last-Modified" : "Sun Jan 01 00:00:00 1995", |
| 66 |
}, |
| 67 |
"Response": 304, |
| 68 |
"ExpectHeader" : { |
| 69 |
"If-Modified-Since" : "Thu, 02 Feb 1995 02:02:02 GMT" |
| 70 |
}, |
| 71 |
} |
| 72 |
|
| 73 |
Outdated_Rules = { |
| 74 |
"SendHeader" : { |
| 75 |
# RFC850 format |
| 76 |
"Last-Modified" : "Thursday, 01-Jan-15 00:00:00 GMT", |
| 77 |
}, |
| 78 |
"ExpectHeader" : { |
| 79 |
"If-Modified-Since" : "Mon, 01 Jan 1990 00:00:00 GMT", |
| 80 |
}, |
| 81 |
} |
| 82 |
|
| 83 |
UpToDate_Server_File1 = WgetFile ("UpToDateFile1", Cont3, rules=UpToDate_Rules1) |
| 84 |
UpToDate_Server_File2 = WgetFile ("UpToDateFile2", Cont3, rules=UpToDate_Rules2) |
| 85 |
UpToDate_Server_File3 = WgetFile ("UpToDateFile3", Cont3, rules=UpToDate_Rules3) |
| 86 |
Newer_Server_File = WgetFile ("NewerFile", Cont3, rules=Newer_Rules) |
| 87 |
Updated_Server_File = WgetFile ("UpdatedFile", Cont4, rules=Outdated_Rules) |
| 88 |
|
| 89 |
WGET_OPTIONS = "-N" |
| 90 |
WGET_URLS = [["UpToDateFile1", "UpToDateFile2", "UpToDateFile3", "NewerFile", |
| 91 |
"UpdatedFile", ]] |
| 92 |
|
| 93 |
Files = [[UpToDate_Server_File1, UpToDate_Server_File2, UpToDate_Server_File3, |
| 94 |
Newer_Server_File, Updated_Server_File, ]] |
| 95 |
|
| 96 |
Existing_Files = [UpToDate_Local_File1, UpToDate_Local_File2, |
| 97 |
UpToDate_Local_File3, Newer_Local_File, Outdated_Local_File] |
| 98 |
|
| 99 |
ExpectedReturnCode = 0 |
| 100 |
|
| 101 |
# The up-to-date file should not be downloaded |
| 102 |
ExpectedDownloadedFiles = [UpToDate_Local_File1, UpToDate_Local_File2, |
| 103 |
UpToDate_Local_File3, Newer_Local_File, |
| 104 |
Updated_Server_File] |
| 105 |
|
| 106 |
# Kind of hack to ensure proper request types |
| 107 |
Request_List = [ |
| 108 |
[ |
| 109 |
"GET /UpToDateFile1", |
| 110 |
"GET /UpToDateFile2", |
| 111 |
"GET /UpToDateFile3", |
| 112 |
"GET /NewerFile", |
| 113 |
"GET /UpdatedFile", |
| 114 |
] |
| 115 |
] |
| 116 |
|
| 117 |
################ Pre and Post Test Hooks ##################################### |
| 118 |
pre_test = { |
| 119 |
"ServerFiles" : Files, |
| 120 |
"LocalFiles" : Existing_Files |
| 121 |
} |
| 122 |
test_options = { |
| 123 |
"WgetCommands" : WGET_OPTIONS, |
| 124 |
"Urls" : WGET_URLS |
| 125 |
} |
| 126 |
post_test = { |
| 127 |
"ExpectedFiles" : ExpectedDownloadedFiles, |
| 128 |
"ExpectedRetcode" : ExpectedReturnCode, |
| 129 |
"FilesCrawled" : Request_List, |
| 130 |
} |
| 131 |
|
| 132 |
err = HTTPTest ( |
| 133 |
pre_hook=pre_test, |
| 134 |
test_params=test_options, |
| 135 |
post_hook=post_test |
| 136 |
).begin () |
| 137 |
|
| 138 |
exit (err) |