Last active
April 8, 2020 22:22
-
-
Save TimNZ/93c94888eef7e2bee67860238929d60d to your computer and use it in GitHub Desktop.
Revisions
-
TimNZ revised this gist
Apr 8, 2020 . 1 changed file with 34 additions and 27 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,44 +1,51 @@ const axios = require("axios").default const axiosCookieJarSupport = require("axios-cookiejar-support").default const CookieJar = require("tough-cookie").CookieJar const assert = require('assert') let instanceId = 1 const createAxiosInstance = () => { const axiosInstance = axios.create({ baseURL: "https://test.site/wp-json/cocart/v1", withCredentials: true, }) axiosCookieJarSupport(axiosInstance) axiosInstance.defaults.jar = new CookieJar() axiosInstance.instanceId = instanceId++ return axiosInstance } const getCart = async (axiosInstance) => axiosInstance.get("/get-cart").then((response) => { console.log(axiosInstance.instanceId, "get-cart", response.data) return response.data }) const addItem = async (axiosInstance, productId) => axiosInstance .post("/add-item", { product_id: productId, }) .then((response) => { console.log(axiosInstance.instanceId, "add-item", response.data) return response.data }) const clearCart = async (axiosInstance) => axiosInstance.post("/clear").then((response) => { console.log(axiosInstance.instanceId, "clear", response.data) return response.data }) async function run(axiosInstance) { let cart = await getCart(axiosInstance) assert(cart.length == 0) await addItem(axiosInstance, 1) cart = await getCart(axiosInstance) assert(cart.totals.subtotal == '$10.00') await addItem(axiosInstance, 2) cart = await getCart(axiosInstance) assert(cart.totals.subtotal == '$20.00') } run(createAxiosInstance()) run(createAxiosInstance()) -
TimNZ created this gist
Apr 8, 2020 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,44 @@ const axios = require('axios').default const axiosCookieJarSupport = require('axios-cookiejar-support').default; const CookieJar = require('tough-cookie').CookieJar; let instanceId = 1 const createAxiosInstance = () => { const axiosInstance = axios.create({ baseURL: 'https://test.site/wp-json/cocart/v1', withCredentials: true, }); axiosCookieJarSupport(axiosInstance) axiosInstance.defaults.jar = new CookieJar() axiosInstance.instanceId = instanceId++ return axiosInstance } const getCart = async (axiosInstance) => axiosInstance.get('/get-cart') .then(response => { console.log(axiosInstance.instanceId,'get-cart',response.data) }) .catch(err => console.log(err)) const addItem = async (axiosInstance,productId) => axiosInstance.post('/add-item',{ product_id: productId }) .then(response => { console.log(axiosInstance.instanceId,'add-item',response.data); return response.headers}) const clearCart = async (axiosInstance) => axiosInstance.post('/clear') .then(response => console.log(axiosInstance.instanceId,'clear',response.data)); async function run(axiosInstance) { await getCart(axiosInstance) await addItem(axiosInstance,1) await getCart(axiosInstance) await addItem(axiosInstance,2) await getCart(axiosInstance) } run(createAxiosInstance()) run(createAxiosInstance())