Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

res.json() result prototype issue #2133

Closed
julien-sugg opened this issue Sep 10, 2021 · 2 comments · Fixed by #2135
Closed

res.json() result prototype issue #2133

julien-sugg opened this issue Sep 10, 2021 · 2 comments · Fixed by #2135
Labels
Milestone

Comments

@julien-sugg
Copy link

julien-sugg commented Sep 10, 2021

Greetings,

I started playing with K6 for Smoke Testing purposes, and I am facing some issues with my internal utils that leverage Object.prototype.hasOwnProperty.call() to perform some assertions in my workflow on the res.json() HTTP response data structure, whom I believe doesn't work as expected.

Environment

  • k6 version: k6 v0.33.0 (2021-06-29T10:38:31+0000/cc0361c, go1.16.5, linux/amd64)
  • OS and version: WSL2 with Debian
  • Docker version and image, if applicable: N/A

Expected Behavior

Object.prototype.hasOwnProperty.call( jsonResponse, 'length' ) to be true.

Actual Behavior

Object.prototype.hasOwnProperty.call( jsonResponse, 'length' ) is false.

Steps to Reproduce the Problem

  1. Use the following simplified snippet that re-uses the official example's crocodiles API
import http from 'k6/http';
import { check, sleep, fail } from 'k6';

const hasLength = ( value: unknown ): boolean => Object.prototype.hasOwnProperty.call( value, 'length' ) && ( value as any ).length > 0;
const isArrayFull = ( value: unknown ): value is unknown[] => Array.isArray( value ) && hasLength( value );

export default function main(): void {
  const baseArray = [
    'foo',
    'bar',
    'baz',
  ];

  const res = http.get( 'https://test-api.k6.io/public/crocodiles/' );
  const crocs = res.json();
  check( res, {
    'is status 200': ( r ) => r.status === 200,
    // Always fails even when the requests is successful
    'has crocodiles info': () => isArrayFull( crocs ),
  } );

  // Dummy cond to get rid of TS errors and to avoid non-null assertion operator
  if ( !crocs ) fail( 'no croc found' );

  // Dummy console log to reproduce:

  // 1. baseArray reference properly constructed
  console.log( 'baseArray Object.prototype.hasOwnProperty.call', Object.prototype.hasOwnProperty.call( baseArray, 'length' ) );
  console.log( 'baseArray hasOwnProperty (not recommended due to eventual DOS):', baseArray.hasOwnProperty( 'length' ) );
  console.log( 'baseArray is array:', Array.isArray( baseArray ) );
  console.log( 'baseArray length:', baseArray.length );

  // 2. response array built from res.json()
  console.log( 'crocs Object.prototype.hasOwnProperty.call', Object.prototype.hasOwnProperty.call( crocs, 'length' ) );
  console.log( 'crocs hasOwnProperty (not recommended due to eventual DOS):', crocs.hasOwnProperty( 'length' ) );
  console.log( 'crocs is array:', Array.isArray( crocs ) );
  console.log( 'crocs length:', crocs.length );

  sleep( 1 );
}
  1. Run the script without any specific parameters (do not forget to transpile as it's typescript code)
k6 run ./k6-issue/dist/has-own-prop.test.ts.
  1. Confirm that the API's response result is fasle (crocs Object.prototype.hasOwnProperty.call false) despite the fact that the property length exists, while the baseArray reference is true (baseArray Object.prototype.hasOwnProperty.call true)
    3.1. Confirm that the has crocodiles info check also fails
...
INFO[0001] baseArray Object.prototype.hasOwnProperty.call true  source=console
INFO[0001] baseArray hasOwnProperty (not recommended due to eventual DOS): true  source=console
INFO[0001] baseArray is array: true                      source=console
INFO[0001] baseArray length: 3                           source=console
INFO[0001] crocs Object.prototype.hasOwnProperty.call false  source=console
INFO[0001] crocs hasOwnProperty (not recommended due to eventual DOS): false  source=console
INFO[0001] crocs is array: true                          source=console
INFO[0001] crocs length: 8                               source=console

running (00m01.5s), 0/1 VUs, 1 complete and 0 interrupted iterations
default ✓ [======================================] 1 VUs  00m01.5s/10m0s  1/1 iters, 1 per VU

     ✓ is status 200
     ✗ has crocodiles info
      ↳  0% — ✓ 0 / ✗ 1
...

Please let me know if you need any additional information.

Thanks.

@mstoykov
Copy link
Contributor

This seemed to have been broken since v0.28.0 and even before that (IMO) it was working because it was hitting the one case it worked in, but with #2135 it should work again 🤞

@na-- na-- added this to the v0.35.1 milestone Sep 13, 2021
mstoykov added a commit that referenced this issue Sep 13, 2021
@julien-sugg
Copy link
Author

This seemed to have been broken since v0.28.0 and even before that (IMO) it was working because it was hitting the one case it worked in, but with #2135 it should work again 🤞

I confirm that it works with the freshly released v0.34.1 (2021-09-16T08:24:44+0000/0628db0, go1.16.8, linux/amd64) that includes #2135 changeset.

Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants