request.test.js 4.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
const PAGE_PATH = '/pages/API/request/request'


const methodMap = {
  "GET": "/api/http/method/get",
  "POST": "/api/http/method/post",
  "PUT": "/api/http/method/put",
  "DELETE": "/api/http/method/delete",
  "PATCH": "/api/http/method/patch",
  "OPTIONS": "/api/http/method/options",
  "HEAD": "/api/http/method/head"
}

async function request(page, method, header, data, url) {
  if (url == null) {
    url = methodMap[method]
  }
  await page.setData({
    url: url,
    method: method,
    data: data,
    header: header
  })
  res = await page.callMethod('jest_request')
25
  await page.waitFor(2000);
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
  res = await page.data('jest_result');
  expect(res).toBe(true)
}

describe('ExtApi-Request', () => {
  let page;
  let res;

  beforeAll(async () => {
    page = await program.reLaunch(PAGE_PATH)
    await page.waitFor(600);
  });


  beforeEach(async () => {
    await page.setData({
42 43
      jest_result: false,
      data: null,
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
      header: null
    })
  });


  it('Check GET', async () => {
    await request(page, 'GET');
  });
  it('Check POST ContentType Json', async () => {
    await request(page, 'POST', {
      "Content-Type": "application/json"
    }, {
      "hello": "world"
    }, "/api/http/contentType/json");
  });
  it('Check POST ContentType Form', async () => {
    await request(page, 'POST', {
      "Content-Type": "application/x-www-form-urlencoded"
    }, "hello=world", "/api/http/contentType/xWwwFormUrlencoded");
  });
  it('Check PUT', async () => {
    await request(page, 'PUT');
  });
  it('Check DELETE', async () => {
    await request(page, 'DELETE');
  });
  it('Check PATCH', async () => {
    await request(page, 'PATCH');
72
  });
雪洛's avatar
雪洛 已提交
73 74 75
  if (process.env.uniTestPlatformInfo.indexOf('web') === -1) {
    it('Check OPTIONS', async () => {
      await request(page, 'OPTIONS');
76
    });
雪洛's avatar
雪洛 已提交
77
  }
78 79
  it('Check HEAD', async () => {
    await request(page, 'HEAD');
80
  });
81 82 83 84 85
  it('Request with timeout null', async () => {
    res = await page.callMethod('jest_timeout_null')
    await page.waitFor(2000);
    res = await page.data('jest_result');
    expect(res).toBe(true)
86 87 88 89 90 91 92
  });
  it('Get Array', async () => {
    res = await page.callMethod('jest_get_array')
    await page.waitFor(2000);
    res = await page.data('jest_result');
    expect(res).toBe(true)
  })
93 94

  let shouldTestCookie = false
95
  if (process.env.uniTestPlatformInfo.startsWith('android') && !process.env.UNI_AUTOMATOR_APP_WEBVIEW) {
96 97 98
    let version = process.env.uniTestPlatformInfo
    version = parseInt(version.split(" ")[1])
    shouldTestCookie = version > 9
99 100
  }

101
  if (process.env.uniTestPlatformInfo.toLocaleLowerCase().startsWith('ios') && !process.env.UNI_AUTOMATOR_APP_WEBVIEW) {
102
    shouldTestCookie = true
103 104 105 106
  }

  if (!shouldTestCookie) {
    return
107
  }
108 109 110 111 112 113 114 115 116 117 118 119

  it('Check Set Cookie', async () => {
    res = await page.callMethod('jest_set_cookie')
    await page.waitFor(2000);
    res = await page.data('jest_result');
    expect(res).toBe(true)
  });
  it('Check Delete Cookie', async () => {
    res = await page.callMethod('jest_delete_cookie')
    await page.waitFor(2000);
    res = await page.data('jest_result');
    expect(res).toBe(true)
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
  });
  it('Check Set Cookie Expires', async () => {
    await page.callMethod('jest_set_cookie_expires')
    await page.waitFor(2000);
    res = await page.data('jest_result_data');
    console.log("request expires cookie data :", res);
    res = await page.data('jest_result');
    expect(res).toBe(true)
    await page.setData({
      jest_result: false,
      jest_result_data: "",
      data: null,
      header: null
    })
    await page.waitFor(5000);
    await page.callMethod('jest_cookie_request', false)
    await page.waitFor(2000);
    res = await page.data('jest_result_data');
    console.log("verify request data :", res);
    res = await page.data('jest_result');
    expect(res).toBe(true)
  });
142
  it('Check Get With Data', async () => {
143 144 145
    res = await page.callMethod('jest_get_with_data')
    await page.waitFor(2000);
    res = await page.data('jest_result');
146
    expect(res).toBe(true)
147 148 149 150 151 152
  })
  it('Check Get With Generics', async () => {
    res = await page.callMethod('jest_get_with_generics')
    await page.waitFor(2000);
    res = await page.data('jest_result');
    expect(res).toBe(true)
153
  })
154

155
  // 15以下的模拟器所对应的xcode不能编译自定义插件,大于15是因为某台设备,会用xcode14.1跑15.5的设备
156 157 158
  let version = process.env.uniTestPlatformInfo
  let split = version.split(" ")
  version = parseInt(split[split.length - 1])
159
  if(!process.env.uniTestPlatformInfo.toLocaleLowerCase().startsWith('ios') || version > 15) {
160 161 162 163 164 165 166
    it('Check Post In UTS Module', async () => {
      res = await page.callMethod('jest_uts_module_invoked')
      await page.waitFor(2000);
      res = await page.data('jest_result');
      expect(res).toBe(true)
    })
  }
167
});