api-request.js 666 B

123456789101112131415161718192021222324
  1. // @ts-check
  2. import http from 'k6/http';
  3. import { check, fail } from 'k6';
  4. export class ShopApiRequest {
  5. constructor(fileName) {
  6. this.document = open('../graphql/' + fileName);
  7. }
  8. post(variables = {}) {
  9. const res = http.post('http://localhost:3000/shop-api/', {
  10. query: this.document,
  11. variables: JSON.stringify(variables),
  12. });
  13. check(res, {
  14. 'Did not error': r => r.json().errors == null && r.status === 200,
  15. });
  16. const result = res.json();
  17. if (result.errors) {
  18. fail('Errored: ' + result.errors[0].message);
  19. }
  20. return res.json();
  21. }
  22. }