1
1
import base64
2
2
import cookielib
3
3
import re
4
+ import os
4
5
import traceback
5
6
import urllib
6
7
import sys
10
11
from saml2 import BINDING_HTTP_POST
11
12
from saml2 .request import SERVICE2REQUEST
12
13
from saml2 .sigver import signed_instance_factory , pre_signature_part
14
+ from saml2 .samlp import HttpParameters
13
15
14
16
from saml2test import CheckError , FatalError
15
17
from saml2test .check import Check
31
33
32
34
logger = logging .getLogger (__name__ )
33
35
36
+ FILE_EXT = {"text/html" : "html" , "test/plain" : "txt" , "application/json" : "json" ,
37
+ "text/xml" : "xml" , "application/xml" : "xml" , }
38
+
34
39
camel2underscore = re .compile ('((?<=[a-z0-9])[A-Z]|(?!^)[A-Z](?=[a-z]))' )
35
40
36
41
@@ -47,6 +52,7 @@ def __init__(self, instance, config, interaction, json_config,
47
52
self .check_factory = check_factory
48
53
self .msg_factory = msg_factory
49
54
self .expect_exception = expect_exception
55
+ self .commandlineargs = commandlineargs
50
56
51
57
self .cjar = {"browser" : cookielib .CookieJar (),
52
58
"rp" : cookielib .CookieJar (),
@@ -144,8 +150,46 @@ def which_endpoint(self, url):
144
150
return None
145
151
146
152
def _log_response (self , response ):
153
+ """Depending on -k argument write content to either logger or extra file
154
+
155
+ Create the <operation> directory; delete all possibly existing files
156
+ Write response content into response_x.<ext> (with x incrementing from 0)
157
+ """
147
158
logger .info ("<-- Status: %s" % response .status_code )
148
- logger .info ("<-- Content: %s" % response .content )
159
+ if response .status_code in [302 , 301 , 303 ]:
160
+ logger .info ("<-- location: %s" %
161
+ response .headers ._store ['location' ][1 ])
162
+ else :
163
+ if self .commandlineargs .content_log :
164
+ self ._content_log_fileno = getattr (self , '_content_log_fileno' , 0 ) + 1
165
+ if not getattr (self , 'logcontentpath' , None ):
166
+ try :
167
+ content_type_hdr = response .headers ._store ['content-type' ][1 ]
168
+ l = content_type_hdr .split (';' ) + ['charset=ISO-8859-1' ,]
169
+ content_type = l [0 ]
170
+ encoding = l [1 ].split ("=" )
171
+ ext = "." + FILE_EXT [content_type ]
172
+ except Exception as e :
173
+ ext = ""
174
+ self ._logcontentpath = os .path .join (
175
+ self .commandlineargs .logpath , "log" ,
176
+ self .commandlineargs .oper )
177
+ if not os .path .exists (self ._logcontentpath ):
178
+ os .makedirs (self ._logcontentpath )
179
+ for fn in os .listdir (self ._logcontentpath ):
180
+ old_file = os .path .join (self ._logcontentpath , fn )
181
+ if os .path .isfile (old_file ):
182
+ os .unlink (old_file )
183
+ fn = os .path .join (self ._logcontentpath , "response_%d%s"
184
+ % (self ._content_log_fileno , ext ))
185
+ f = open (fn , "w" )
186
+ f .write (response .content )
187
+ f .close ()
188
+ logger .info ("<-- Response content (encoding=%s) in file %s" %
189
+ (encoding , fn ))
190
+ pass
191
+ else :
192
+ logger .info ("<-- Content: %s" % response .content )
149
193
150
194
def wb_send_GET_startpage (self ):
151
195
"""
@@ -193,6 +237,8 @@ def parse_saml_message(self):
193
237
self .saml_request = self .instance ._parse_request (
194
238
_str , SERVICE2REQUEST [self ._endpoint ], self ._endpoint ,
195
239
self ._binding )
240
+ if self ._binding == BINDING_HTTP_REDIRECT :
241
+ self .http_parameters = HttpParameters (_dict )
196
242
197
243
def _redirect (self , _response ):
198
244
rdseq = []
@@ -363,7 +409,7 @@ def do_sequence_and_tests(self, oper, tests=None):
363
409
self .do_flow (flow )
364
410
except InteractionNeeded :
365
411
self .test_output .append ({"status" : INTERACTION ,
366
- "message" : self . last_content ,
412
+ "message" : "see detail log for response content" ,
367
413
"id" : "exception" ,
368
414
"name" : "interaction needed" ,
369
415
"url" : self .position })
0 commit comments