@tarojs/plugin-http 3.7.0-beta.3 → 3.7.0-beta.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/runtime.js CHANGED
@@ -609,7 +609,7 @@ _XMLHttpRequest_method = new WeakMap(), _XMLHttpRequest_url = new WeakMap(), _XM
609
609
  __classPrivateFieldGet(this, _XMLHttpRequest_instances, "m", _XMLHttpRequest_callReadyStateChange).call(this, XMLHttpRequest.HEADERS_RECEIVED);
610
610
  if (ENABLE_COOKIE) {
611
611
  // 处理 set-cookie
612
- const setCookieStr = header['Set-Cookie'];
612
+ const setCookieStr = this.getResponseHeader('set-cookie');
613
613
  if (setCookieStr && typeof setCookieStr === 'string') {
614
614
  let start = 0;
615
615
  let startSplit = 0;
@@ -637,11 +637,12 @@ _XMLHttpRequest_method = new WeakMap(), _XMLHttpRequest_url = new WeakMap(), _XM
637
637
  // 处理返回数据
638
638
  if (data) {
639
639
  __classPrivateFieldGet(this, _XMLHttpRequest_instances, "m", _XMLHttpRequest_callReadyStateChange).call(this, XMLHttpRequest.LOADING);
640
- const loadstartEvent = createXMLHttpRequestEvent('loadstart', this, header['Content-Length']);
640
+ const contentLength = Number(this.getResponseHeader('content-length') || 0);
641
+ const loadstartEvent = createXMLHttpRequestEvent('loadstart', this, contentLength);
641
642
  this.trigger('loadstart', loadstartEvent);
642
643
  isFunction(this.onloadstart) && this.onloadstart(loadstartEvent);
643
644
  __classPrivateFieldSet(this, _XMLHttpRequest_response, data, "f");
644
- const loadEvent = createXMLHttpRequestEvent('load', this, header['Content-Length']);
645
+ const loadEvent = createXMLHttpRequestEvent('load', this, contentLength);
645
646
  this.trigger('load', loadEvent);
646
647
  isFunction(this.onload) && this.onload(loadEvent);
647
648
  }
@@ -677,7 +678,8 @@ _XMLHttpRequest_method = new WeakMap(), _XMLHttpRequest_url = new WeakMap(), _XM
677
678
  __classPrivateFieldSet(this, _XMLHttpRequest_requestTask, null, "f");
678
679
  __classPrivateFieldGet(this, _XMLHttpRequest_instances, "m", _XMLHttpRequest_callReadyStateChange).call(this, XMLHttpRequest.DONE);
679
680
  if (__classPrivateFieldGet(this, _XMLHttpRequest_status, "f")) {
680
- const loadendEvent = createXMLHttpRequestEvent('loadend', this, __classPrivateFieldGet(this, _XMLHttpRequest_header, "f")['Content-Length']);
681
+ const contentLength = Number(this.getResponseHeader('content-length') || 0);
682
+ const loadendEvent = createXMLHttpRequestEvent('loadend', this, contentLength);
681
683
  this.trigger('loadend', loadendEvent);
682
684
  isFunction(this.onloadend) && this.onloadend(loadendEvent);
683
685
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tarojs/plugin-http",
3
- "version": "3.7.0-beta.3",
3
+ "version": "3.7.0-beta.4",
4
4
  "description": "Taro 小程序端支持使用 web 请求 的插件",
5
5
  "main": "index.js",
6
6
  "files": [
@@ -23,9 +23,9 @@
23
23
  },
24
24
  "homepage": "https://github.com/NervJS/taro#readme",
25
25
  "dependencies": {
26
- "@tarojs/runtime": "3.7.0-beta.3",
27
- "@tarojs/shared": "3.7.0-beta.3",
28
- "@tarojs/service": "3.7.0-beta.3"
26
+ "@tarojs/runtime": "3.7.0-beta.4",
27
+ "@tarojs/service": "3.7.0-beta.4",
28
+ "@tarojs/shared": "3.7.0-beta.4"
29
29
  },
30
30
  "devDependencies": {
31
31
  "@rollup/plugin-json": "^6.0.0",
@@ -0,0 +1,101 @@
1
+ import { window } from '@tarojs/runtime'
2
+ import { request, RequestTask } from '@tarojs/taro'
3
+
4
+ import { XMLHttpRequest, XMLHttpRequestEvent } from '../../dist/runtime'
5
+
6
+ jest.mock('@tarojs/taro', () => {
7
+ return {
8
+ request: jest.fn(),
9
+ }
10
+ })
11
+
12
+ const requestMock = jest.mocked(request)
13
+
14
+ describe('XMLHttpRequest', () => {
15
+ it('大写header key', async () => {
16
+ const data = 'success'
17
+ // 模拟实现 request 函数
18
+ requestMock.mockImplementation((opt) => {
19
+ if (opt.success) {
20
+ const header = { 'Content-Length': data.length.toString() }
21
+ opt.success({ data: data as any, header, statusCode: 200, errMsg: '' })
22
+ }
23
+ if (opt.complete) {
24
+ opt.complete({ errMsg: '' })
25
+ }
26
+ return {} as RequestTask<any>
27
+ })
28
+ // 发起请求
29
+ const xhr = new XMLHttpRequest()
30
+ xhr.open('GET', 'localhost')
31
+ const req = new Promise<XMLHttpRequestEvent>((resolve, reject) => {
32
+ xhr.addEventListener('load', resolve)
33
+ xhr.addEventListener('error', reject)
34
+ })
35
+ xhr.send('')
36
+ await req
37
+ // 检查是否能正常读取大写的header
38
+ expect(xhr.status).toBe(200)
39
+ expect(data.length).toBeGreaterThan(0)
40
+ expect(xhr.getResponseHeader('Content-length')).toBe(data.length.toString())
41
+ expect(xhr.getResponseHeader('content-length')).toBe(data.length.toString())
42
+ expect(xhr.getResponseHeader('Content-Length')).toBe(data.length.toString())
43
+ })
44
+ it('小写header key', async () => {
45
+ const data = 'success'
46
+ // 模拟实现 request 函数
47
+ requestMock.mockImplementation((opt) => {
48
+ if (opt.success) {
49
+ const header = { 'content-length': data.length.toString() }
50
+ opt.success({ data: data as any, header, statusCode: 200, errMsg: '' })
51
+ }
52
+ if (opt.complete) {
53
+ opt.complete({ errMsg: '' })
54
+ }
55
+ return {} as RequestTask<any>
56
+ })
57
+ const xhr = new XMLHttpRequest()
58
+ xhr.open('GET', 'localhost')
59
+ const req = new Promise<XMLHttpRequestEvent>((resolve, reject) => {
60
+ xhr.addEventListener('load', resolve)
61
+ xhr.addEventListener('error', reject)
62
+ })
63
+ xhr.send('')
64
+ await req
65
+ // 检查是否能成功读取小写的header
66
+ expect(xhr.status).toBe(200)
67
+ expect(xhr.responseText).toBe(data)
68
+ expect(xhr.getResponseHeader('Content-length')).toBe(data.length.toString())
69
+ expect(xhr.getResponseHeader('content-length')).toBe(data.length.toString())
70
+ expect(xhr.getResponseHeader('Content-Length')).toBe(data.length.toString())
71
+ })
72
+
73
+ it('set-cookie', async () => {
74
+ const data = 'success'
75
+ // 模拟实现 request 函数
76
+ requestMock.mockImplementation((opt) => {
77
+ if (opt.success) {
78
+ const header = { 'set-cookie': 'aaa=bbb; domain=taro.com' }
79
+ opt.success({ data: data as any, header, statusCode: 200, errMsg: '' })
80
+ }
81
+ if (opt.complete) {
82
+ opt.complete({ errMsg: '' })
83
+ }
84
+ return {} as RequestTask<any>
85
+ })
86
+ // 发送一个请求
87
+ const xhr = new XMLHttpRequest()
88
+ xhr.open('GET', 'localhost')
89
+ const req = new Promise<XMLHttpRequestEvent>((resolve, reject) => {
90
+ xhr.addEventListener('load', resolve)
91
+ xhr.addEventListener('error', reject)
92
+ })
93
+ // 在发送请求之前cookie为空
94
+ expect(window.document.cookie.length).toBe(0)
95
+ xhr.send('')
96
+ // 等待请求加载完成
97
+ await req
98
+ // 检查是否能读取set-cookie
99
+ expect(window.document.cookie.length).not.toBe(0)
100
+ })
101
+ })
@@ -262,7 +262,7 @@ export class XMLHttpRequest extends Events {
262
262
 
263
263
  if (ENABLE_COOKIE) {
264
264
  // 处理 set-cookie
265
- const setCookieStr = header['Set-Cookie']
265
+ const setCookieStr = this.getResponseHeader('set-cookie')
266
266
 
267
267
  if (setCookieStr && typeof setCookieStr === 'string') {
268
268
  let start = 0
@@ -297,12 +297,13 @@ export class XMLHttpRequest extends Events {
297
297
  // 处理返回数据
298
298
  if (data) {
299
299
  this.#callReadyStateChange(XMLHttpRequest.LOADING)
300
- const loadstartEvent = createXMLHttpRequestEvent('loadstart', this, header['Content-Length'])
300
+ const contentLength = Number(this.getResponseHeader('content-length') || 0)
301
+ const loadstartEvent = createXMLHttpRequestEvent('loadstart', this, contentLength)
301
302
  this.trigger('loadstart', loadstartEvent)
302
303
  isFunction(this.onloadstart) && this.onloadstart(loadstartEvent)
303
304
  this.#response = data
304
305
 
305
- const loadEvent = createXMLHttpRequestEvent('load', this, header['Content-Length'])
306
+ const loadEvent = createXMLHttpRequestEvent('load', this, contentLength)
306
307
  this.trigger('load', loadEvent)
307
308
  isFunction(this.onload) && this.onload(loadEvent)
308
309
  }
@@ -349,7 +350,8 @@ export class XMLHttpRequest extends Events {
349
350
  this.#callReadyStateChange(XMLHttpRequest.DONE)
350
351
 
351
352
  if (this.#status) {
352
- const loadendEvent = createXMLHttpRequestEvent('loadend', this, this.#header['Content-Length'])
353
+ const contentLength = Number(this.getResponseHeader('content-length') || 0)
354
+ const loadendEvent = createXMLHttpRequestEvent('loadend', this, contentLength)
353
355
  this.trigger('loadend', loadendEvent)
354
356
  isFunction(this.onloadend) && this.onloadend(loadendEvent)
355
357
  }