Best Python code snippet using gabbi_python
test_blocks_request.py
Source:test_blocks_request.py
1from fuzzowski import constants2from fuzzowski.mutants.spike import *3import hashlib4import pytest5def test_request_spike():6 s_initialize('request_test_complex')7 with s_block('b0'):8 with s_block('b1'):9 s_string(b'str1', name='str1')10 s_static(b' ')11 s_checksum('b1', algorithm='md5', output_format='hex', name='md5_b1')12 s_delim(b':')13 s_size('b0', inclusive=True, output_format='binary', length=4, endian=constants.BIG_ENDIAN)14 s_byte(b'\xff')15 s_static(b'.')16 s_variable('var1', b'NOTSET')17 s_repeat('b0', min_reps=0, max_reps=2)18 request = s_get('request_test_complex')19 b1_rendered = (b'str1 '20 + hashlib.md5(b'str1 ').digest().hex().encode('utf-8')21 + b':')22 assert request.render() == (b1_rendered23 + b'\x00\x00\x00\x2a'24 + b'\xff'25 + b'.'26 + b'NOTSET')27 request.goto(request.num_mutations) # last mutation28 assert request.render() == (b1_rendered29 + b'\x00\x00\x00\x2a'30 + b'\xff'31 + b'.'32 + b'NOTSET'33 + b1_rendered * 2)34 request.reset()35 request.variables['var1'] = b'SET!!!'36 assert request.render() == (b1_rendered37 + b'\x00\x00\x00\x2a'38 + b'\xff'39 + b'.'40 + b'SET!!!')41def test_request_reset():42 s_initialize('request_test_simple')43 s_mutant(b'A', name='mutant21', mutations=[b'B', b'C', b'D'])44 s_mutant(b'W', name='mutant22', mutations=[b'X', b'Y', b'Z'])45 test_request = s_get('request_test_simple')46 assert test_request.render() == b'AW'47 assert test_request.mutant_index == 048 assert next(test_request) == b'BW' == test_request.render()49 assert test_request.mutant_index == 150 assert next(test_request) == b'CW' == test_request.render()51 assert test_request.mutant_index == 252 assert test_request.mutant.name == 'mutant21'53 assert next(test_request) == b'DW' == test_request.render()54 assert test_request.mutant_index == 355 assert test_request.mutant.name == 'mutant21'56 assert next(test_request) == b'AX' == test_request.render()57 assert test_request.mutant_index == 458 assert test_request.mutant.name == 'mutant22'59 assert next(test_request) == b'AY' == test_request.render()60 assert test_request.mutant_index == 561 assert next(test_request) == b'AZ' == test_request.render()62 assert test_request.mutant_index == 663 assert test_request.mutant.name == 'mutant22'64 with pytest.raises(StopIteration):65 next(test_request)66 assert test_request.mutant_index == 067 assert test_request.render() == b'AW'68 assert test_request.mutant is None69 assert next(test_request) == b'BW' == test_request.render()70 assert test_request.mutant_index == 171 assert test_request.mutant.name == 'mutant21'72 test_request.reset()73 assert test_request.mutant_index == 074 assert test_request.render() == b'AW'75 assert test_request.mutant is None76 assert next(test_request) == b'BW' == test_request.render()77 assert test_request.mutant_index == 178 assert test_request.mutant.name == 'mutant21'79def test_request_goto():80 s_initialize('request_test_simple2')81 s_mutant(b'A', name='mutant21', mutations=[b'B', b'C', b'D'])82 s_mutant(b'W', name='mutant22', mutations=[b'X', b'Y', b'Z'])83 test_request = s_get('request_test_simple2')84 assert test_request.render() == b'AW'85 assert test_request.mutant_index == 086 assert next(test_request) == b'BW' == test_request.render()87 assert test_request.mutant_index == 188 assert next(test_request) == b'CW' == test_request.render()89 assert test_request.mutant_index == 290 assert test_request.mutant.name == 'mutant21'91 assert next(test_request) == b'DW' == test_request.render()92 assert test_request.mutant_index == 393 assert test_request.mutant.name == 'mutant21'94 assert next(test_request) == b'AX' == test_request.render()95 assert test_request.mutant_index == 496 assert test_request.mutant.name == 'mutant22'97 assert next(test_request) == b'AY' == test_request.render()98 assert test_request.mutant_index == 599 assert next(test_request) == b'AZ' == test_request.render()100 assert test_request.mutant_index == 6101 assert test_request.mutant.name == 'mutant22'102 with pytest.raises(StopIteration):103 next(test_request)104 assert test_request.mutant_index == 0105 assert test_request.render() == b'AW'106 assert test_request.mutant is None107 assert next(test_request) == b'BW' == test_request.render()108 assert test_request.mutant_index == 1109 assert test_request.mutant.name == 'mutant21'110 test_request.reset()111 assert test_request.mutant_index == 0112 assert test_request.render() == b'AW'113 assert test_request.mutant is None114 assert next(test_request) == b'BW' == test_request.render()115 assert test_request.mutant_index == 1116 assert test_request.mutant.name == 'mutant21'117def test_request_movements():118 s_initialize('request_test_complex2')119 with s_block('b0'):120 with s_block('b1'):121 s_string(b'str1', name='str1')122 s_static(b' ')123 s_checksum('b1', algorithm='md5', output_format='hex', name='md5_b1')124 s_delim(b':')125 s_size('b0', inclusive=True, output_format='binary', length=4, endian=constants.BIG_ENDIAN)126 s_byte(b'\xff')127 s_static(b'.')128 s_variable('var1', b'NOTSET')129 s_repeat('b0', min_reps=0, max_reps=2)130 request = s_get('request_test_complex2')131 i=0132 for mutation in request:133 i+=1134 assert request.mutant_index == i135 assert request.mutant.mutant_index != 0136 for item in request.stack:137 if item != request.mutant:138 if not isinstance(item, blocks.Block):139 assert item.mutant_index == 0140 else:141 assert item.mutant_index != 0142 for item in request.stack:...
file_server_test.py
Source:file_server_test.py
...16 s=socket.socket(socket.AF_INET,socket.SOCK_STREAM);17 s.connect(('localhost',8080));18 s.setsockopt(socket.IPPROTO_TCP,socket.TCP_NODELAY,1)19 return s;20def test_request(url,status,content='ignore',valid=[],notvalid=[]):21 print "-- Testing %s" % url22 s=make_sock();23 s.send('GET %s HTTP/1.0\r\n\r\n' % url);24 text = ''25 while 1:26 tmp = s.recv(1000);27 if len(tmp) == 0:28 break;29 text = text + tmp;30 exp = 'HTTP/1.0 ' + str(status) + ' '31 test(text[:len(exp)]==exp)32 parts = text.split('\r\n\r\n');33 real_content = ''34 if len(parts)>=2:35 real_content = parts[1]36 if content != 'ignore':37 test(real_content == content + '\n')38 for v in valid:39 test(real_content.find(v) >= 0)40 for v in notvalid:41 test(real_content.find(v) == -1)42do_listing = False 43if len(sys.argv) == 2:44 do_listing = sys.argv[1] == 'listing'45print "- Testing normal requests"46if not do_listing:47 test_request('/',404)48else:49 test_request('/',200,valid=['foo/','bar/','test.txt'],notvalid=['..','test.txt/','.svn'])50 51test_request('/test.txt',200,'/test.txt')52test_request('/foo/test.txt',200,'/foo/test.txt')53test_request('/bar/test.txt',200,'/bar/test.txt')54test_request('/bar/index.html',200,'/bar/index.html')55test_request('/bar/',200,'/bar/index.html')56test_request('/bar',302)57if not do_listing:58 test_request('/foo',404)59 test_request('/foo/',404)60else:61 test_request('/foo',302)62 test_request('/foo/',200,valid=['..','ooooooong_fiiiiii']);63test_request('/file+with+space.txt',200,'file with space')64test_request('/file%20with%20space.txt',200,'file with space')65print "- Testing alias"66test_request('/alias',302)67test_request('/alias/',200,'/al/index.html')68test_request('/alias/test.txt',200,'/al/test.txt')69test_request('/alias/foo/test.txt',200,'/al/foo/test.txt')70if os.name == 'posix':71 print "- Testing symlinks"72 test_request('/no.txt',404)73 test_request('/yes.txt',200,'/yes')74print "- Testing directory traversal"75test_request('/foo/../bar/test.txt',200,'/bar/test.txt')76test_request('/../al/test.txt','404')77test_request('/../never.txt','404')78test_request('/../www/test.txt','404')79test_request('/../wwwfile.txt','404')80test_request('/aliasfile.txt',404)81test_request('/../alias/never.txt','404')82test_request('/%2e%2e/never.txt','404')...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!