@@ -139,32 +139,32 @@ def parse(self, base_url: str, testcase_list: List, test_file=None, working_dire
139
139
140
140
elif key == YamlKeyWords .TEST :
141
141
with ChangeDir (working_directory ):
142
- __group_name = None
143
- for node_dict in sub_testcase_node :
144
- if __group_name is None :
145
- __group_name = node_dict .get (TestCaseKeywords .group )
146
-
147
- __group_name = __group_name if __group_name else TestCaseGroup .DEFAULT_GROUP
148
- try :
149
- group_object = TestSet .test_group_list_dict [__group_name ]
150
- except KeyError :
151
- group_object = TestCaseGroup (TestCaseGroup .DEFAULT_GROUP , config = testcase_config_object )
152
- TestSet .test_group_list_dict [__group_name ] = group_object
153
-
154
- testcase_object = TestCase (
155
- base_url = base_url , extract_binds = group_object .extract_binds ,
156
- variable_binds = group_object .variable_binds , context = group_object .context ,
157
- config = group_object .config
158
- )
159
- testcase_object .parse (sub_testcase_node )
160
- group_object .testcase_list = testcase_object
142
+ self .parse_test (base_url , sub_testcase_node , testcase_config_object )
161
143
162
144
elif key == YamlKeyWords .CONFIG :
163
145
testcase_config_object .parse (sub_testcase_node )
164
146
165
147
self .config = testcase_config_object
166
148
167
- return
149
+ @staticmethod
150
+ def parse_test (base_url , sub_testcase_node , testcase_config_object ):
151
+ __group_name = None
152
+ for node_dict in sub_testcase_node :
153
+ if __group_name is None :
154
+ __group_name = node_dict .get (TestCaseKeywords .group )
155
+ __group_name = __group_name if __group_name else TestCaseGroup .DEFAULT_GROUP
156
+ try :
157
+ group_object = TestSet .test_group_list_dict [__group_name ]
158
+ except KeyError :
159
+ group_object = TestCaseGroup (TestCaseGroup .DEFAULT_GROUP , config = testcase_config_object )
160
+ TestSet .test_group_list_dict [__group_name ] = group_object
161
+ testcase_object = TestCase (
162
+ base_url = base_url , extract_binds = group_object .extract_binds ,
163
+ variable_binds = group_object .variable_binds , context = group_object .context ,
164
+ config = group_object .config
165
+ )
166
+ testcase_object .parse (sub_testcase_node )
167
+ group_object .testcase_list = testcase_object
168
168
169
169
170
170
class TestCaseGroup :
@@ -482,8 +482,7 @@ def set_template(self, variable_name, template_string):
482
482
def body (self ):
483
483
if isinstance (self .__body , str ) or self .__body is None :
484
484
return self .__body
485
- else :
486
- return self .__body .get_content (context = self .__context )
485
+ return self .__body .get_content (context = self .__context )
487
486
488
487
@body .setter
489
488
def body (self , value ):
@@ -553,15 +552,12 @@ def parse(self, testcase_dict):
553
552
if self .http_method in ["POST" , "PUT" , "DELETE" ]:
554
553
self .expected_http_status_code_list = [200 , 201 , 204 ]
555
554
556
- return
557
-
558
555
def pre_update (self , context ):
559
556
if self .variable_binds :
560
557
context .bind_variables (self .variable_binds )
561
558
if self .generator_binds :
562
559
for key , value in self .generator_binds .items ():
563
560
context .bind_generator_next (key , value )
564
- return
565
561
566
562
def post_update (self , context ):
567
563
if self .extract_binds :
@@ -570,21 +566,17 @@ def post_update(self, context):
570
566
body = self .body , headers = self .headers , context = context )
571
567
if result :
572
568
context .bind_variable (key , result )
573
- return
574
569
575
570
def is_dynamic (self ):
576
- if self .templates :
577
- return True
578
- if isinstance (self .__body , ContentHandler ) and self .__body .is_dynamic ():
571
+ if self .templates or (isinstance (self .__body , ContentHandler ) and self .__body .is_dynamic ()):
579
572
return True
573
+ return False
580
574
581
575
def render (self ):
582
576
if self .is_dynamic () or self .__context is not None :
583
577
if isinstance (self .__body , ContentHandler ):
584
578
self .__body = self .__body .get_content (self .__context )
585
579
586
- return
587
-
588
580
def __perform_validation (self ) -> List :
589
581
590
582
failure_list = []
@@ -621,13 +613,7 @@ def run(self, context=None, timeout=None, curl_handler=None):
621
613
else :
622
614
curl_handler = pycurl .Curl ()
623
615
624
- body_byte = BytesIO ()
625
- header_byte = BytesIO ()
626
- curl_handler .setopt (curl_handler .URL , str (self .url ))
627
- curl_handler .setopt (curl_handler .TIMEOUT , timeout )
628
- curl_handler .setopt (pycurl .WRITEFUNCTION , body_byte .write )
629
- curl_handler .setopt (pycurl .HEADERFUNCTION , header_byte .write )
630
- curl_handler .setopt (pycurl .VERBOSE , self .__verbose )
616
+ body_byte , header_byte = self .__default_curl_config (curl_handler , timeout )
631
617
if self .config .timeout :
632
618
curl_handler .setopt (pycurl .CONNECTTIMEOUT , self .config .timeout )
633
619
@@ -642,44 +628,10 @@ def run(self, context=None, timeout=None, curl_handler=None):
642
628
if self .auth_username and self .auth_password :
643
629
curl_handler .setopt (pycurl .USERPWD , self .auth_username + ':' + self .auth_password )
644
630
645
- body_length = len (self .body ) if self .body else 0
646
- if self .http_method == EnumHttpMethod .POST .name :
647
- curl_handler .setopt (EnumHttpMethod .POST .value , 1 )
648
- curl_handler .setopt (pycurl .POSTFIELDSIZE , body_length )
649
-
650
- elif self .http_method == EnumHttpMethod .PUT .name :
651
- curl_handler .setopt (EnumHttpMethod .PUT .value , 1 )
652
- curl_handler .setopt (pycurl .INFILESIZE , body_length )
653
-
654
- elif self .http_method == EnumHttpMethod .PATCH .name :
655
- curl_handler .setopt (EnumHttpMethod .PATCH .value , EnumHttpMethod .PATCH .name )
656
- curl_handler .setopt (pycurl .POSTFIELDS , self .body )
657
-
658
- elif self .http_method == EnumHttpMethod .DELETE .name :
659
- curl_handler .setopt (EnumHttpMethod .DELETE .value , EnumHttpMethod .DELETE .name )
660
- if self .body :
661
- curl_handler .setopt (pycurl .POSTFIELDS , self .body )
662
- curl_handler .setopt (pycurl .POSTFIELDSIZE , body_length )
663
-
664
- elif self .http_method == EnumHttpMethod .HEAD .name :
665
- curl_handler .setopt (pycurl .NOBODY , 1 )
666
- curl_handler .setopt (EnumHttpMethod .HEAD .value , EnumHttpMethod .HEAD .name )
667
- else :
668
- curl_handler .setopt (pycurl .CUSTOMREQUEST , self .http_method .upper ())
669
- if self .body :
670
- curl_handler .setopt (pycurl .POSTFIELDS , self .body )
671
- curl_handler .setopt (pycurl .POSTFIELDSIZE , body_length )
631
+ self .__configure_curl_method (curl_handler )
672
632
673
633
head = self .headers
674
- if head .get ('content-type' ):
675
- content_type = head ['content-type' ]
676
- head [u'content-type' ] = content_type + ' ; charset=UTF-8'
677
-
678
- headers = [str (header_name ) + ':' + str (header_value ) for header_name , header_value in head .items ()]
679
- headers .append ("Expect:" )
680
- headers .append ("Connection: close" )
681
- logger .debug ("Request headers %s " % head )
682
- curl_handler .setopt (curl_handler .HTTPHEADER , headers )
634
+ self .__configure_curl_headers (curl_handler , head )
683
635
684
636
if self .__delay :
685
637
time .sleep (self .__delay )
@@ -694,14 +646,12 @@ def run(self, context=None, timeout=None, curl_handler=None):
694
646
self .__failure_list .append (
695
647
Failure (message = "Curl Exception: {0}" .format (e ), details = trace , failure_type = FAILURE_CURL_EXCEPTION ))
696
648
return
697
- body = body_byte .getvalue ()
649
+ self . body = body_byte .getvalue ()
698
650
body_byte .close ()
699
- logger .debug ("RESPONSE: %s" % self .body )
700
651
response_code = curl_handler .getinfo (pycurl .RESPONSE_CODE )
701
- self .body = body
702
652
self .__response_code = int (response_code )
703
653
if self .config .print_bodies :
704
- print (body )
654
+ print (self . body )
705
655
try :
706
656
response_headers = Parser .parse_headers (header_byte .getvalue ())
707
657
self .__response_headers = response_headers
@@ -730,3 +680,53 @@ def run(self, context=None, timeout=None, curl_handler=None):
730
680
Failure (message = failure_message , details = None , failure_type = FAILURE_INVALID_RESPONSE )
731
681
)
732
682
curl_handler .close ()
683
+
684
+ @staticmethod
685
+ def __configure_curl_headers (curl_handler , head ):
686
+ if head .get ('content-type' ):
687
+ content_type = head ['content-type' ]
688
+ head [u'content-type' ] = '%s ; charset=UTF-8' % content_type
689
+ headers = ["%s:%s" % (header_name , header_value ) for header_name , header_value in head .items ()]
690
+ headers .append ("Expect:" )
691
+ headers .append ("Connection: close" )
692
+ logger .debug ("Request headers %s " % head )
693
+ curl_handler .setopt (curl_handler .HTTPHEADER , headers )
694
+
695
+ def __configure_curl_method (self , curl_handler ):
696
+ body_length = len (self .body ) if self .body else 0
697
+ if self .http_method == EnumHttpMethod .POST .name :
698
+ curl_handler .setopt (EnumHttpMethod .POST .value , 1 )
699
+ curl_handler .setopt (pycurl .POSTFIELDSIZE , body_length )
700
+
701
+ elif self .http_method == EnumHttpMethod .PUT .name :
702
+ curl_handler .setopt (EnumHttpMethod .PUT .value , 1 )
703
+ curl_handler .setopt (pycurl .INFILESIZE , body_length )
704
+
705
+ elif self .http_method == EnumHttpMethod .PATCH .name :
706
+ curl_handler .setopt (EnumHttpMethod .PATCH .value , EnumHttpMethod .PATCH .name )
707
+ curl_handler .setopt (pycurl .POSTFIELDS , self .body )
708
+
709
+ elif self .http_method == EnumHttpMethod .DELETE .name :
710
+ curl_handler .setopt (EnumHttpMethod .DELETE .value , EnumHttpMethod .DELETE .name )
711
+ if self .body :
712
+ curl_handler .setopt (pycurl .POSTFIELDS , self .body )
713
+ curl_handler .setopt (pycurl .POSTFIELDSIZE , body_length )
714
+
715
+ elif self .http_method == EnumHttpMethod .HEAD .name :
716
+ curl_handler .setopt (pycurl .NOBODY , 1 )
717
+ curl_handler .setopt (EnumHttpMethod .HEAD .value , EnumHttpMethod .HEAD .name )
718
+ else :
719
+ curl_handler .setopt (pycurl .CUSTOMREQUEST , self .http_method .upper ())
720
+ if self .body :
721
+ curl_handler .setopt (pycurl .POSTFIELDS , self .body )
722
+ curl_handler .setopt (pycurl .POSTFIELDSIZE , body_length )
723
+
724
+ def __default_curl_config (self , curl_handler , timeout ):
725
+ body_byte = BytesIO ()
726
+ header_byte = BytesIO ()
727
+ curl_handler .setopt (curl_handler .URL , str (self .url ))
728
+ curl_handler .setopt (curl_handler .TIMEOUT , timeout )
729
+ curl_handler .setopt (pycurl .WRITEFUNCTION , body_byte .write )
730
+ curl_handler .setopt (pycurl .HEADERFUNCTION , header_byte .write )
731
+ curl_handler .setopt (pycurl .VERBOSE , self .__verbose )
732
+ return body_byte , header_byte
0 commit comments