http.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. import Vue from 'vue';
  2. describe('Vue.http', function () {
  3. it('get("data/text.txt")', done => {
  4. Vue.http.get('data/text.txt').then(res => {
  5. expect(res.ok).toBe(true);
  6. expect(res.status).toBe(200);
  7. expect(res.body).toBe('text');
  8. expect(res.headers.get('Content-Type')).toBe('text/plain');
  9. expect(res.headers.get('Content-Length')).toBe('4');
  10. done();
  11. });
  12. });
  13. it('get("data/valid.json")', done => {
  14. Vue.http.get('data/valid.json').then(res => {
  15. expect(res.ok).toBe(true);
  16. expect(res.status).toBe(200);
  17. expect(typeof res.body).toBe('object');
  18. expect(res.body.foo).toBe('bar');
  19. done();
  20. });
  21. });
  22. it('get("data/invalid.json")', done => {
  23. Vue.http.get('data/invalid.json').then(res => {
  24. expect(res.ok).toBe(true);
  25. expect(res.status).toBe(200);
  26. expect(res.body).toBeNull();
  27. done();
  28. });
  29. });
  30. it('get("github.com/avatar")', done => {
  31. Vue.http.get('https://avatars1.githubusercontent.com/u/6128107', {responseType: 'blob'}).then(res => {
  32. expect(res.ok).toBe(true);
  33. expect(res.status).toBe(200);
  34. expect(res.body instanceof Blob).toBe(true);
  35. expect(res.body.type).toBe('image/png');
  36. done();
  37. });
  38. });
  39. it('get("cors-api.appspot.com")', done => {
  40. Vue.http.get('http://server.cors-api.appspot.com/server?id=1&enable=true').then(res => {
  41. expect(res.ok).toBe(true);
  42. expect(res.status).toBe(200);
  43. expect(typeof res.body).toBe('object');
  44. expect(res.body.shift().requestType).toBe('cors');
  45. if (res.headers.get('Content-Type')) {
  46. expect(res.headers.get('Content-Type')).toBe('application/json');
  47. }
  48. done();
  49. });
  50. });
  51. it('jsonp("jsfiddle.net/jsonp")', done => {
  52. Vue.http.jsonp('http://jsfiddle.net/echo/jsonp/', {params: {foo: 'bar'}}).then(res => {
  53. expect(res.ok).toBe(true);
  54. expect(res.status).toBe(200);
  55. expect(typeof res.body).toBe('object');
  56. expect(res.body.foo).toBe('bar');
  57. done();
  58. });
  59. });
  60. });
  61. describe('this.$http', function () {
  62. it('get("data/valid.json")', done => {
  63. var vm = new Vue({
  64. created() {
  65. this.$http.get('data/valid.json').then(res => {
  66. expect(this).toBe(vm);
  67. expect(res.ok).toBe(true);
  68. expect(res.status).toBe(200);
  69. expect(typeof res.body).toBe('object');
  70. expect(res.body.foo).toBe('bar');
  71. done();
  72. });
  73. }
  74. });
  75. });
  76. it('get("data/valid.json") with timeout', done => {
  77. var vm = new Vue({
  78. created() {
  79. var random = Math.random().toString(36).substr(2);
  80. this.$http.get(`data/valid.json?${random}`, {timeout: 1}).then(res => {
  81. fail('Callback has been called');
  82. }, res => {
  83. expect(res.ok).toBe(false);
  84. expect(res.status).toBe(0);
  85. done();
  86. });
  87. }
  88. });
  89. });
  90. it('get("data/valid.json") with abort()', done => {
  91. var vm = new Vue({
  92. created() {
  93. var random = Math.random().toString(36).substr(2);
  94. this.$http.get(`data/valid.json?${random}`, {
  95. before(req) {
  96. setTimeout(() => {
  97. expect(typeof req.abort).toBe('function');
  98. req.abort();
  99. }, 0);
  100. }
  101. }).then(res => {
  102. fail('Callback has been called');
  103. }, res => {
  104. done();
  105. });
  106. }
  107. });
  108. });
  109. it('get("data/notfound.json") using catch()', done => {
  110. var vm = new Vue({
  111. created() {
  112. this.$http.get('data/notfound.json').catch(res => {
  113. expect(this).toBe(vm);
  114. expect(res.ok).toBe(false);
  115. expect(res.status).toBe(404);
  116. done();
  117. });
  118. }
  119. });
  120. });
  121. });