From 9e7656980243d5200a604ad2867c5eefb26e23ce Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=AB=98=E6=9B=A6?= Date: Fri, 21 Jul 2023 06:16:53 +0000 Subject: [PATCH] update Http.fetch.test.js. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 高曦 --- .../src/main/js/test/Http.fetch.test.js | 725 ++++++++++-------- 1 file changed, 412 insertions(+), 313 deletions(-) diff --git a/telephony/telephonyjstest/netmanager_base/system_fetch/src/main/js/test/Http.fetch.test.js b/telephony/telephonyjstest/netmanager_base/system_fetch/src/main/js/test/Http.fetch.test.js index 18eb95bf2..36fd36e42 100644 --- a/telephony/telephonyjstest/netmanager_base/system_fetch/src/main/js/test/Http.fetch.test.js +++ b/telephony/telephonyjstest/netmanager_base/system_fetch/src/main/js/test/Http.fetch.test.js @@ -1,4 +1,3 @@ - /** * Copyright (C) 2022-2023 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License") @@ -15,330 +14,430 @@ */ import fetch from '@system.fetch'; -import { describe, it, expect } from '@ohos/hypium' +import { describe, expect, it } from '@ohos/hypium' + export default function Telephony_NetStack_HttpFetchTest() { - describe("Telephony_NetStack_HttpFetchTest", function () { + describe("Telephony_NetStack_HttpFetchTest", function () { + + /** + * @tc.number Telephony_NetStack_HttpTestFetchBasicGet_0100 + * @tc.name 基本场景,FETCH方式测试能否GET一个网址 + * @tc.desc function test + */ + it("Telephony_NetStack_HttpTestFetchBasicGet_0100", 0, function (done) { + fetchTest(); + var objectTimeOut = setTimeout(function timeout() { + fetchTest(); + }, 30000); + + function fetchTest() { + fetch.fetch({ + url: "https://httpbin.org/anything", + success: function (data) { + console.info("NetStack fetch success " + JSON.stringify(data)); + expect(data.code === 200 || data.code > 500).assertTrue(); + clearTimeout(objectTimeOut); + done(); + }, + fail: function () { + console.info("NetStack Fetch Failed"); + expect().assertFail(); + clearTimeout(objectTimeOut); + done(); + }, + complete: function () { + console.info("NetStack Fetch Complete"); + } + }) + } + }); + + /** + * @tc.number Telephony_NetStack_HttpTestFetchUserAgent_0100 + * @tc.name FETCH方式返回用户代理 + * @tc.desc function test + */ + it("Telephony_NetStack_HttpTestFetchUserAgent_0100", 0, function (done) { + fetchTest(); + var objectTimeOut = setTimeout(function timeout() { + fetchTest(); + }, 30000); - /** - * @tc.number Telephony_NetStack_HttpTestFetchBasicGet_0100 - * @tc.name 基本场景,FETCH方式测试能否GET一个网址 - * @tc.desc function test - */ - it("Telephony_NetStack_HttpTestFetchBasicGet_0100", 0, function (done) { - fetch.fetch({ - url : "https://httpbin.org/anything", - success : function(data){ - console.info("NetStack fetch success "+JSON.stringify(data)); - expect(data.code === 200 || data.code > 500).assertTrue(); - done(); - }, - fail : function(){ - console.info("NetStack Fetch Failed"); - expect().assertFail(); - done(); - }, - complete : function(){ - console.info("NetStack Fetch Complete"); - } - }) - }); + function fetchTest() { + fetch.fetch({ + url: "https://httpbin.org/user-agent", + success: function (data) { + console.info("NetStack fetch success " + JSON.stringify(data)); + expect(data.code === 200 || data.code > 500).assertTrue(); + if (data.code === 200) { + expect(JSON.parse(data.data.toString())["user-agent"] == "libcurl-agent/1.0").assertTrue(); + } + clearTimeout(objectTimeOut); + done(); + }, + fail: function () { + console.info("NetStack Fetch Failed"); + expect().assertFail(); + clearTimeout(objectTimeOut); + done(); + }, + complete: function () { + console.info("NetStack fetch complete"); + } + }) + } + }); + + /** + * @tc.number Telephony_NetStack_HttpTestFetchUserAgent_0200 + * @tc.name FETCH方式返回用户代理 + * @tc.desc function test + */ + it("Telephony_NetStack_HttpTestFetchUserAgent_0200", 0, function (done) { + fetchTest(); + var objectTimeOut = setTimeout(function timeout() { + fetchTest(); + }, 30000); - /** - * @tc.number Telephony_NetStack_HttpTestFetchUserAgent_0100 - * @tc.name FETCH方式返回用户代理 - * @tc.desc function test - */ - it("Telephony_NetStack_HttpTestFetchUserAgent_0100", 0, function (done) { - fetch.fetch({ - url : "https://httpbin.org/user-agent", - success : function(data){ - console.info("NetStack fetch success "+ JSON.stringify(data)); - expect(data.code === 200 || data.code > 500).assertTrue(); - if (data.code === 200) { - expect(JSON.parse(data.data.toString())["user-agent"] == "libcurl-agent/1.0").assertTrue(); - } - done(); - }, - fail : function(){ - console.info("NetStack Fetch Failed"); - expect().assertFail(); - done(); - }, - complete : function(){ - console.info("NetStack fetch complete"); - } - }) - }); + function fetchTest() { + fetch.fetch({ + url: "https://httpbin.org/user-agent", + responseType: "json", + success: function (data) { + console.info("NetStack fetch success " + JSON.stringify(data)); + expect(data.code === 200 || data.code > 500).assertTrue(); + if (data.code === 200) { + expect(data.data["user-agent"] == "libcurl-agent/1.0").assertTrue(); + } + clearTimeout(objectTimeOut); + done(); + }, + fail: function () { + console.info("NetStack Fetch Failed"); + expect().assertFail(); + clearTimeout(objectTimeOut); + done(); + }, + complete: function () { + console.info("NetStack fetch complete"); + } + }) + } + }); - /** - * @tc.number Telephony_NetStack_HttpTestFetchUserAgent_0200 - * @tc.name FETCH方式返回用户代理 - * @tc.desc function test - */ - it("Telephony_NetStack_HttpTestFetchUserAgent_0200", 0, function (done) { - fetch.fetch({ - url : "https://httpbin.org/user-agent", - responseType : "json", - success : function(data){ - console.info("NetStack fetch success "+ JSON.stringify(data)); - expect(data.code === 200 || data.code > 500).assertTrue(); - if (data.code === 200) { - expect(data.data["user-agent"] == "libcurl-agent/1.0").assertTrue(); - } - done(); - }, - fail : function(){ - console.info("NetStack Fetch Failed"); - expect().assertFail(); - done(); - }, - complete : function(){ - console.info("NetStack fetch complete"); - } - }) - }); + /** + * @tc.number Telephony_NetStack_HttpTestFetchHeader_0100 + * @tc.name 返回Header + * @tc.desc function test + */ + it("Telephony_NetStack_HttpTestFetchHeader_0100", 0, function (done) { + fetchTest(); + var objectTimeOut = setTimeout(function timeout() { + fetchTest(); + }, 30000); - /** - * @tc.number Telephony_NetStack_HttpTestFetchHeader_0100 - * @tc.name 返回Header - * @tc.desc function test - */ - it("Telephony_NetStack_HttpTestFetchHeader_0100", 0, function (done) { - fetch.fetch({ - url : "https://httpbin.org/headers", - success : function(data){ - console.info("NetStack Fetch Success "+ JSON.stringify(data)); - expect(data.code === 200 || data.code > 500).assertTrue(); - if (data.code === 200) { - expect(JSON.parse(data.data.toString())["headers"]["Content-Type"] === "application/json").assertTrue(); - } - done(); - }, - fail : function(){ - console.info("NetStack Fetch Failed"); - expect().assertFail(); - done(); - }, - complete : function(){ - console.info("NetStack Fetch Complete"); - } - }) - }); + function fetchTest() { + fetch.fetch({ + url: "https://httpbin.org/headers", + success: function (data) { + console.info("NetStack Fetch Success " + JSON.stringify(data)); + expect(data.code === 200 || data.code > 500).assertTrue(); + if (data.code === 200) { + expect(JSON.parse(data.data.toString())["headers"]["Content-Type"] === "application/json").assertTrue(); + } + clearTimeout(objectTimeOut); + done(); + }, + fail: function () { + console.info("NetStack Fetch Failed"); + expect().assertFail(); + clearTimeout(objectTimeOut); + done(); + }, + complete: function () { + console.info("NetStack Fetch Complete"); + } + }) + } + }); - /** - * @tc.number Telephony_NetStack_HttpTestFetchHeader_0200 - * @tc.name 返回Header - * @tc.desc function test - */ - it("Telephony_NetStack_HttpTestFetchHeader_0200", 0, function (done) { - fetch.fetch({ - url : "https://httpbin.org/headers", - responseType : "json", - success : function(data){ - console.info("NetStack Fetch Success "+ JSON.stringify(data)); - expect(data.code === 200 || data.code > 500).assertTrue(); - if (data.code === 200) { - expect(data.data["headers"]["Content-Type"] === "application/json").assertTrue(); - } - done(); - }, - fail : function(){ - console.info("NetStack Fetch Failed"); - expect().assertFail(); - done(); - }, - complete : function(){ - console.info("NetStack Fetch Complete"); - } - }) - }); + /** + * @tc.number Telephony_NetStack_HttpTestFetchHeader_0200 + * @tc.name 返回Header + * @tc.desc function test + */ + it("Telephony_NetStack_HttpTestFetchHeader_0200", 0, function (done) { + fetchTest(); + var objectTimeOut = setTimeout(function timeout() { + fetchTest(); + }, 30000); - /** - * @tc.number Telephony_NetStack_HttpTestFetchPost_0100 - * @tc.name 返回Post内容 - * @tc.desc function test - */ - it("Telephony_NetStack_HttpTestFetchPost_0100", 0, function (done) { - fetch.fetch({ - url : "https://httpbin.org/post", - method : "POST", - data : "MineMine", - success : function(data){ - console.info("NetStack "+ JSON.stringify(data)); - expect(data.code === 200 || data.code > 500).assertTrue(); - if (data.code === 200) { - expect(JSON.parse(data.data.toString())["data"] === "MineMine").assertTrue(); - } - done(); - }, - fail : function(){ - console.info("NetStack Fetch Failed"); - expect().assertFail(); - done() - }, - complete : function(){ - console.info("NetStack Fetch Complete"); - } - }) - }); + function fetchTest() { + fetch.fetch({ + url: "https://httpbin.org/headers", + responseType: "json", + success: function (data) { + console.info("NetStack Fetch Success " + JSON.stringify(data)); + expect(data.code === 200 || data.code > 500).assertTrue(); + if (data.code === 200) { + expect(data.data["headers"]["Content-Type"] === "application/json").assertTrue(); + } + clearTimeout(objectTimeOut); + done(); + }, + fail: function () { + console.info("NetStack Fetch Failed"); + expect().assertFail(); + clearTimeout(objectTimeOut); + done(); + }, + complete: function () { + console.info("NetStack Fetch Complete"); + } + }) + } + }); - /** - * @tc.number Telephony_NetStack_HttpTestFetchPost_0200 - * @tc.name 返回Post内容 - * @tc.desc function test - */ - it("Telephony_NetStack_HttpTestFetchPost_0200", 0, function (done) { - fetch.fetch({ - url : "https://httpbin.org/post", - method : "POST", - data : "MineMine", - responseType : "json", - success : function(data){ - console.info("NetStack "+ JSON.stringify(data)); - expect(data.code === 200 || data.code > 500).assertTrue(); - if (data.code === 200) { - expect(data.data["data"] === "MineMine").assertTrue(); - } - done(); - }, - fail : function(){ - console.info("NetStack Fetch Failed"); - expect().assertFail(); - done(); - }, - complete : function(){ - console.info("NetStack Fetch Complete"); - } - }) - }); + /** + * @tc.number Telephony_NetStack_HttpTestFetchPost_0100 + * @tc.name 返回Post内容 + * @tc.desc function test + */ + it("Telephony_NetStack_HttpTestFetchPost_0100", 0, function (done) { + fetchTest(); + var objectTimeOut = setTimeout(function timeout() { + fetchTest(); + }, 30000); - /** - * @tc.number Telephony_NetStack_HttpTestFetchPut_0100 - * @tc.name 返回Put内容 - * @tc.desc function test - */ - it("Telephony_NetStack_HttpTestFetchPut_0100", 0, function (done) { - fetch.fetch({ - url : "https://httpbin.org/put", - method : "PUT", - data : "MineMine", - success : function(data){ - console.info("NetStack "+ JSON.stringify(data)); - expect(data.code === 200 || data.code > 500).assertTrue(); - if (data.code === 200) { - expect(JSON.parse(data.data.toString())["data"] === "MineMine").assertTrue(); - } - done(); - }, - fail : function(){ - console.info("NetStack Fetch Failed"); - expect().assertFail(); - done(); - }, - complete : function(){ - console.info("NetStack Fetch Complete"); - } - }) - }); + function fetchTest() { + fetch.fetch({ + url: "https://httpbin.org/post", + method: "POST", + data: "MineMine", + success: function (data) { + console.info("NetStack " + JSON.stringify(data)); + expect(data.code === 200 || data.code > 500).assertTrue(); + if (data.code === 200) { + expect(JSON.parse(data.data.toString())["data"] === "MineMine").assertTrue(); + } + clearTimeout(objectTimeOut); + done(); + }, + fail: function () { + console.info("NetStack Fetch Failed"); + expect().assertFail(); + clearTimeout(objectTimeOut); + done() + }, + complete: function () { + console.info("NetStack Fetch Complete"); + } + }) + } + }); - /** - * @tc.number Telephony_NetStack_HttpTestFetchPut_0200 - * @tc.name 返回Put内容 - * @tc.desc function test - */ - it("Telephony_NetStack_HttpTestFetchPut_0200", 0, function (done) { - fetch.fetch({ - url : "https://httpbin.org/put", - method : "PUT", - data : "MineMine", - responseType : "json", - success : function(data){ - console.info("NetStack "+ JSON.stringify(data)); - if (data.code === 200) { - expect(data.code === 200 || data.code > 500).assertTrue(); - expect(data.data["data"] === "MineMine").assertTrue(); - } - done(); - }, - fail : function(){ - console.info("NetStack Fetch Failed"); - expect().assertFail(); - done(); - }, - complete : function(){ - console.info("NetStack Fetch Complete"); - } - }) - }); + /** + * @tc.number Telephony_NetStack_HttpTestFetchPost_0200 + * @tc.name 返回Post内容 + * @tc.desc function test + */ + it("Telephony_NetStack_HttpTestFetchPost_0200", 0, function (done) { + fetchTest(); + var objectTimeOut = setTimeout(function timeout() { + fetchTest(); + }, 30000); + + function fetchTest() { + fetch.fetch({ + url: "https://httpbin.org/post", + method: "POST", + data: "MineMine", + responseType: "json", + success: function (data) { + console.info("NetStack " + JSON.stringify(data)); + expect(data.code === 200 || data.code > 500).assertTrue(); + if (data.code === 200) { + expect(data.data["data"] === "MineMine").assertTrue(); + } + clearTimeout(objectTimeOut); + done(); + }, + fail: function () { + console.info("NetStack Fetch Failed"); + expect().assertFail(); + clearTimeout(objectTimeOut); + done(); + }, + complete: function () { + console.info("NetStack Fetch Complete"); + } + }) + } + }); + + /** + * @tc.number Telephony_NetStack_HttpTestFetchPut_0100 + * @tc.name 返回Put内容 + * @tc.desc function test + */ + it("Telephony_NetStack_HttpTestFetchPut_0100", 0, function (done) { + fetchTest(); + var objectTimeOut = setTimeout(function timeout() { + fetchTest(); + }, 30000); + + function fetchTest() { + fetch.fetch({ + url: "https://httpbin.org/put", + method: "PUT", + data: "MineMine", + success: function (data) { + console.info("NetStack " + JSON.stringify(data)); + expect(data.code === 200 || data.code > 500).assertTrue(); + if (data.code === 200) { + expect(JSON.parse(data.data.toString())["data"] === "MineMine").assertTrue(); + } + clearTimeout(objectTimeOut); + done(); + }, + fail: function () { + console.info("NetStack Fetch Failed"); + expect().assertFail(); + clearTimeout(objectTimeOut); + done(); + }, + complete: function () { + console.info("NetStack Fetch Complete"); + } + }) + } + }); + + /** + * @tc.number Telephony_NetStack_HttpTestFetchPut_0200 + * @tc.name 返回Put内容 + * @tc.desc function test + */ + it("Telephony_NetStack_HttpTestFetchPut_0200", 0, function (done) { + fetchTest(); + var objectTimeOut = setTimeout(function timeout() { + fetchTest(); + }, 30000); + + function fetchTest() { + fetch.fetch({ + url: "https://httpbin.org/put", + method: "PUT", + data: "MineMine", + responseType: "json", + success: function (data) { + console.info("NetStack " + JSON.stringify(data)); + if (data.code === 200) { + expect(data.code === 200 || data.code > 500).assertTrue(); + expect(data.data["data"] === "MineMine").assertTrue(); + } + clearTimeout(objectTimeOut); + done(); + }, + fail: function () { + console.info("NetStack Fetch Failed"); + expect().assertFail(); + clearTimeout(objectTimeOut); + done(); + }, + complete: function () { + console.info("NetStack Fetch Complete"); + } + }) + } + }); + + /** + * @tc.number Telephony_NetStack_HttpTestFetchUrlEncode_0100 + * @tc.name 返回Cookies内容 + * @tc.desc function test + */ + it("Telephony_NetStack_HttpTestFetchUrlEncode_0100", 0, function (done) { + fetchTest(); + var objectTimeOut = setTimeout(function timeout() { + fetchTest(); + }, 30000); + + function fetchTest() { + fetch.fetch({ + url: "https://httpbin.org/response-headers?key1=val1&key2=val2", + data: { + "name1": "mine1", + "name2": "mine2", + }, + success: function (data) { + console.info("NetStack " + JSON.stringify(data)); + expect(data.code === 200 || data.code > 500).assertTrue(); + if (data.code === 200) { + expect(JSON.parse(data.data.toString())["key1"] === "val1").assertTrue(); + expect(JSON.parse(data.data.toString())["key2"] === "val2").assertTrue(); + expect(JSON.parse(data.data.toString())["name1"] === "mine1").assertTrue(); + expect(JSON.parse(data.data.toString())["name2"] === "mine2").assertTrue(); + } + clearTimeout(objectTimeOut); + done(); + }, + fail: function () { + console.info("NetStack Fetch Failed"); + expect().assertFail(); + clearTimeout(objectTimeOut); + done(); + }, + complete: function () { + console.info("NetStack Fetch Complete"); + } + }) + } + }); - /** - * @tc.number Telephony_NetStack_HttpTestFetchUrlEncode_0100 - * @tc.name 返回Cookies内容 - * @tc.desc function test - */ - it("Telephony_NetStack_HttpTestFetchUrlEncode_0100", 0, function (done) { - fetch.fetch({ - url : "https://httpbin.org/response-headers?key1=val1&key2=val2", - data : { - "name1" : "mine1", - "name2" : "mine2", - }, - success : function(data){ - console.info("NetStack "+JSON.stringify(data)); - expect(data.code === 200 || data.code > 500).assertTrue(); - if (data.code === 200) { - expect(JSON.parse(data.data.toString())["key1"] === "val1").assertTrue(); - expect(JSON.parse(data.data.toString())["key2"] === "val2").assertTrue(); - expect(JSON.parse(data.data.toString())["name1"] === "mine1").assertTrue(); - expect(JSON.parse(data.data.toString())["name2"] === "mine2").assertTrue(); - } - done(); - }, - fail : function(){ - console.info("NetStack Fetch Failed"); - expect().assertFail(); - done(); - }, - complete : function(){ - console.info("NetStack Fetch Complete"); - } - }) - }); + /** + * @tc.number Telephony_NetStack_HttpTestFetchUrlEncode_0200 + * @tc.name 返回Cookies内容 + * @tc.desc function test + */ + it("Telephony_NetStack_HttpTestFetchUrlEncode_0200", 0, function (done) { + fetchTest(); + var objectTimeOut = setTimeout(function timeout() { + fetchTest(); + }, 30000); - /** - * @tc.number Telephony_NetStack_HttpTestFetchUrlEncode_0200 - * @tc.name 返回Cookies内容 - * @tc.desc function test - */ - it("Telephony_NetStack_HttpTestFetchUrlEncode_0200", 0, function (done) { - fetch.fetch({ - url : "https://httpbin.org/response-headers?key1=val1&key2=val2", - data : { - "name1" : "mine1", - "name2" : "mine2", - }, - responseType : "json", - success : function(data){ - console.info("NetStack "+JSON.stringify(data)); - expect(data.code === 200 || data.code > 500).assertTrue(); - if (data.code === 200) { - expect(data.data["key1"] === "val1").assertTrue(); - expect(data.data["key2"] === "val2").assertTrue(); - expect(data.data["name1"] === "mine1").assertTrue(); - expect(data.data["name2"] === "mine2").assertTrue(); - } - done(); - }, - fail : function(){ - console.info("NetStack Fetch Failed"); - expect().assertFail(); - done(); - }, - complete : function(){ - console.info("NetStack Fetch Complete"); - } - }) - }); + function fetchTest() { + fetch.fetch({ + url: "https://httpbin.org/response-headers?key1=val1&key2=val2", + data: { + "name1": "mine1", + "name2": "mine2", + }, + responseType: "json", + success: function (data) { + console.info("NetStack " + JSON.stringify(data)); + expect(data.code === 200 || data.code > 500).assertTrue(); + if (data.code === 200) { + expect(data.data["key1"] === "val1").assertTrue(); + expect(data.data["key2"] === "val2").assertTrue(); + expect(data.data["name1"] === "mine1").assertTrue(); + expect(data.data["name2"] === "mine2").assertTrue(); + } + clearTimeout(objectTimeOut); + done(); + }, + fail: function () { + console.info("NetStack Fetch Failed"); + expect().assertFail(); + clearTimeout(objectTimeOut); + done(); + }, + complete: function () { + console.info("NetStack Fetch Complete"); + } + }) + } }); + }); } \ No newline at end of file -- GitLab