test.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. function expect(left, right) {
  2. if (left !== right) {
  3. console.log("Expect: " + left + " but: " + right);
  4. throw new Error();
  5. }
  6. }
  7. var csrfToken = "";
  8. fly.config.headers = {"x-tag": "flyio"}
  9. fly.config.baseURL = "http://www.dtworkroom.com/doris/1/2.0.0/"
  10. var newFly = new Fly;
  11. newFly.config = fly.config;
  12. var log = console.log
  13. fly.interceptors.request.use(function (request) {
  14. log("request:path:" + request.url + ",baseURL:" + request.baseURL)
  15. if (!csrfToken) {
  16. log("No token,request token firstly...");
  17. // locking the current instance, let the incomming request task enter a
  18. // queue before they enter the request interceptors.
  19. fly.lock();
  20. //Using another fly instance to request csrfToken.
  21. //If use the same fly instance, there may lead a infinite loop:
  22. //(The request will go to the interceptor first, and then
  23. //enter the interceptor again when launching the new request
  24. //in the interceptor....)
  25. return newFly.get("/token").then(function (d) {
  26. request.headers["csrfToken"] = csrfToken = d.data.data.token;
  27. log("token请求成功,值为: " + d.data.data.token);
  28. log("发起请求:path:" + request.url + ",baseURL:" + request.baseURL)
  29. return request
  30. }).finally(function () {
  31. //fly.clear(); //clear the request queue
  32. // unlock the current instance, flush the request queue.
  33. fly.unlock();
  34. })
  35. } else {
  36. request.headers["csrfToken"] = csrfToken;
  37. }
  38. })
  39. describe("request", function () {
  40. it("request", function (done) {
  41. this.timeout(15000);
  42. var data = {
  43. "a": "你好",
  44. "b": [5, "6"],
  45. "c": {"d": 8, "e": {"a": 5, "b": [66, 8]}}
  46. };
  47. var promises = [
  48. fly.get("/test?tag=1")
  49. .then(function (d) {
  50. log("success")
  51. }).catch(function (e) {
  52. log("fail")
  53. }),
  54. fly.get("/test?tag=2")
  55. .then(function (d) {
  56. log("success")
  57. }).catch(function (e) {
  58. log("fail")
  59. }),
  60. fly.get("/test?tag=3")
  61. .then(function (d) {
  62. log("success")
  63. }).catch(function (e) {
  64. log("fail")
  65. }),
  66. fly.get("/test?fm=true", {aa: 8, bb: 9, tt: {xx: 5}}, {
  67. headers: {
  68. "content-type": "application/x-www-form-urlencoded"
  69. }
  70. })
  71. .then(function () {
  72. log("success")
  73. }).catch(function () {
  74. log("fail")
  75. }),
  76. fly.post("/test?fm=true", {aa: 8, bb: 9, tt: {xx: 5}}, {
  77. headers: {
  78. "content-type": "application/x-www-form-urlencoded"
  79. }
  80. })
  81. .then(function () {
  82. log("success")
  83. }).catch(function () {
  84. log("fail")
  85. }),
  86. fly.post("/test?fm=true", {aa: 8, bb: 9, tt: {xx: 5}})
  87. .then(function () {
  88. log("success")
  89. }).catch(function () {
  90. log("fail")
  91. }),
  92. fly.get("http://xxx.bxxcom").catch(function (e) {
  93. log(e.message);
  94. })
  95. ];
  96. fly.all(promises).then(fly.spread(function () {
  97. done()
  98. }))
  99. })
  100. });