basic.js 3.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. import http from "k6/http";
  2. import {check} from "k6";
  3. import names from "./names.js";
  4. const baseUri = `http://petclinic:9966/petclinic/api`;
  5. export default function() {
  6. const specialtiesUrl = `${baseUri}/specialties`;
  7. const specialtiesResponse = http.get(specialtiesUrl);
  8. const specialties = JSON.parse(specialtiesResponse.body);
  9. // Add a new vet to the list
  10. const newVet = names.randomVet(specialties);
  11. const response = http.post(`${baseUri}/vets`, JSON.stringify(newVet),
  12. { headers: { 'Content-Type': 'application/json' } });
  13. // we don't guard against dupes, so this could fail on occasion
  14. check(response, { "create vet status 201": (r) => r.status === 201 });
  15. // make sure we can fetch that vet back out
  16. const vetId = JSON.parse(response.body).id;
  17. const vetUrl = `${baseUri}/vets/${vetId}`
  18. const vetResponse = http.get(vetUrl);
  19. check(vetResponse, { "fetch vet status 200": r => r.status === 200 });
  20. // add a new owner
  21. const newOwner = names.randomOwner();
  22. const newOwnerResponse = http.post(`${baseUri}/owners`, JSON.stringify(newOwner),
  23. { headers: { 'Content-Type': 'application/json' } });
  24. check(newOwnerResponse, { "new owner status 201": r => r.status === 201});
  25. // make sure we can fetch that owner back out
  26. const ownerId = JSON.parse(newOwnerResponse.body).id;
  27. const ownerResponse = http.get(`${baseUri}/owners/${ownerId}`);
  28. check(ownerResponse, { "fetch new owner status 200": r => r.status === 200});
  29. const owner = JSON.parse(ownerResponse.body);
  30. // get the list of all pet types
  31. const petTypes = JSON.parse(http.get(`${baseUri}/pettypes`).body);
  32. const owners = JSON.parse(http.get(`${baseUri}/owners`).body);
  33. // create a 3 new random pets
  34. const pet1 = names.randomPet(petTypes, owner);
  35. const pet2 = names.randomPet(petTypes, owner);
  36. const pet3 = names.randomPet(petTypes, owner);
  37. const petsUrl = `${baseUri}/pets`;
  38. const newPetResponses = http.batch([
  39. ["POST", petsUrl, JSON.stringify(pet1), { headers: { 'Content-Type': 'application/json' } } ],
  40. ["POST", petsUrl, JSON.stringify(pet2), { headers: { 'Content-Type': 'application/json' } } ],
  41. ["POST", petsUrl, JSON.stringify(pet3), { headers: { 'Content-Type': 'application/json' } } ],
  42. ]);
  43. check(newPetResponses[0], { "pet status 201": r => r.status === 201});
  44. check(newPetResponses[1], { "pet status 201": r => r.status === 201});
  45. check(newPetResponses[2], { "pet status 201": r => r.status === 201});
  46. const responses = http.batch([
  47. ["GET", `${baseUri}/pets/${JSON.parse(newPetResponses[0].body).id}`],
  48. ["GET", `${baseUri}/pets/${JSON.parse(newPetResponses[1].body).id}`],
  49. ["GET", `${baseUri}/pets/${JSON.parse(newPetResponses[2].body).id}`]
  50. ]);
  51. check(responses[0], { "pet exists 200": r => r.status === 200});
  52. check(responses[1], { "pet exists 200": r => r.status === 200});
  53. check(responses[2], { "pet exists 200": r => r.status === 200});
  54. // Clean up after ourselves.
  55. // Delete pets
  56. const petDeletes = http.batch([
  57. ["DELETE", `${baseUri}/pets/${JSON.parse(newPetResponses[0].body).id}`],
  58. ["DELETE", `${baseUri}/pets/${JSON.parse(newPetResponses[1].body).id}`],
  59. ["DELETE", `${baseUri}/pets/${JSON.parse(newPetResponses[2].body).id}`]
  60. ]);
  61. check(petDeletes[0], { "pet deleted 204": r => r.status === 204});
  62. check(petDeletes[1], { "pet deleted 204": r => r.status === 204});
  63. check(petDeletes[2], { "pet deleted 204": r => r.status === 204});
  64. // Delete owner
  65. const delOwner = http.del(`${baseUri}/owners/${ownerId}`);
  66. check(delOwner, { "owner deleted 204": r => r.status === 204});
  67. // Delete vet
  68. const delVet = http.del(`${baseUri}/vets/${vetId}`);
  69. check(delVet, { "owner deleted 204": r => r.status === 204});
  70. //TODO: Set up a visit or two
  71. //TODO: Fetch out the owner again because their model has been updated.
  72. };