11import http from 'k6/http' ;
22import { check , sleep } from 'k6' ;
3- import { Rate } from 'k6/metrics' ;
43import {
54 API_CONFIG ,
65 TEST_ADDRESSES ,
@@ -9,15 +8,11 @@ import {
98 FaucetTypes
109} from './test_claim_token_api_config.js' ;
1110
12- // Custom metrics
13- const errorRate = new Rate ( 'errors' ) ;
14-
1511export const options = {
1612 vus : 1 ,
1713 iterations : 1 ,
1814 thresholds : {
1915 'checks' : [ 'rate>=1.0' ] , // 100% of checks MUST pass
20- 'http_req_failed' : [ 'rate<=0.0' ] , // 0% HTTP failures allowed
2116 'http_req_duration' : [ 'p(95)<5000' ] ,
2217 } ,
2318} ;
@@ -67,24 +62,34 @@ function testInputValidation() {
6762 faucetTypes . forEach ( ( faucetType ) => {
6863 TEST_ADDRESSES . INVALID . forEach ( ( invalidAddress , index ) => {
6964 const response = makeClaimRequest ( faucetType , invalidAddress ) ;
70- check ( response , {
71- [ `${ faucetType } - Invalid address "${ invalidAddress } " properly rejected (400 and error)` ] : ( r ) =>
65+ const checkName = `${ faucetType } - Invalid address "${ invalidAddress } " properly rejected (400 and error)` ;
66+ const passed = check ( response , {
67+ [ checkName ] : ( r ) =>
7268 r . status === STATUS_CODES . BAD_REQUEST &&
7369 r . body &&
7470 r . body . toLowerCase ( ) . includes ( "invalid" )
75- } ) || errorRate . add ( 1 ) ;
71+ } ) ;
72+
73+ if ( ! passed ) {
74+ throw new Error ( `❌ ${ checkName } : Expected 400 with 'invalid', got ${ response . status } - ${ response . body } ` ) ;
75+ }
7676 } ) ;
7777 } ) ;
7878
7979 // Test all other invalid request scenarios (missing parameters, mainnet blocking, etc.)
8080 TEST_SCENARIOS . INVALID_REQUESTS . forEach ( ( testCase ) => {
8181 const response = makeClaimRequest ( testCase . faucet_info , testCase . address ) ;
82- check ( response , {
83- [ `${ testCase . name } : properly handled (${ testCase . expectedStatus } + "${ testCase . expectedErrorContains } ")` ] : ( r ) =>
82+ const checkName = `${ testCase . name } : properly handled (${ testCase . expectedStatus } + "${ testCase . expectedErrorContains } ")` ;
83+ const passed = check ( response , {
84+ [ checkName ] : ( r ) =>
8485 r . status === testCase . expectedStatus &&
8586 r . body &&
8687 r . body . toLowerCase ( ) . includes ( testCase . expectedErrorContains . toLowerCase ( ) )
87- } ) || errorRate . add ( 1 ) ;
88+ } ) ;
89+
90+ if ( ! passed ) {
91+ throw new Error ( `❌ ${ checkName } : Expected ${ testCase . expectedStatus } with '${ testCase . expectedErrorContains } ', got ${ response . status } - ${ response . body } ` ) ;
92+ }
8893 } ) ;
8994
9095 console . log ( '✅ Input validation tests completed' ) ;
@@ -97,16 +102,24 @@ function testRateLimiting() {
97102 TEST_SCENARIOS . RATE_LIMIT_TEST_COOLDOWN_CASES . forEach ( testCase => {
98103 const response = makeClaimRequest ( testCase . faucet_info , testCase . address ) ;
99104
100- check ( response , {
101- [ `${ testCase . name } : Expected ${ testCase . expectedStatus } , got ${ response . status } ` ] : ( r ) =>
102- r . status === testCase . expectedStatus ,
103- } ) || errorRate . add ( 1 ) ;
105+ const statusCheckName = `${ testCase . name } : Expected ${ testCase . expectedStatus } , got ${ response . status } ` ;
106+ const statusPassed = check ( response , {
107+ [ statusCheckName ] : ( r ) => r . status === testCase . expectedStatus ,
108+ } ) ;
109+
110+ if ( ! statusPassed ) {
111+ throw new Error ( `❌ ${ statusCheckName } ` ) ;
112+ }
104113
105114 if ( response . status === STATUS_CODES . SUCCESS ) {
106- check ( response , {
107- [ `${ testCase . name } : ✅ Valid transaction hash` ] : ( r ) =>
108- validateTransactionHash ( r . body . trim ( ) ) ,
109- } ) || errorRate . add ( 1 ) ;
115+ const hashCheckName = `${ testCase . name } : ✅ Valid transaction hash` ;
116+ const hashPassed = check ( response , {
117+ [ hashCheckName ] : ( r ) => validateTransactionHash ( r . body . trim ( ) ) ,
118+ } ) ;
119+
120+ if ( ! hashPassed ) {
121+ throw new Error ( `❌ ${ hashCheckName } : Invalid transaction hash format: ${ response . body . trim ( ) } ` ) ;
122+ }
110123 }
111124
112125 // Log results for debugging
@@ -121,4 +134,5 @@ function testRateLimiting() {
121134export default function ( ) {
122135 testInputValidation ( ) ;
123136 testRateLimiting ( ) ;
137+ console . log ( '\n✅ All tests passed successfully!' ) ;
124138}
0 commit comments