未验证 提交 c28b3957 编写于 作者: 高曦 提交者: Gitee

update Http.fetch.test.js.

Signed-off-by: N高曦 <gaoxi785@huawei.com>
上级 9e765698
...@@ -14,6 +14,7 @@ ...@@ -14,6 +14,7 @@
*/ */
import fetch from '@system.fetch'; import fetch from '@system.fetch';
import http from '@ohos.net.http';
import { describe, expect, it } from '@ohos/hypium' import { describe, expect, it } from '@ohos/hypium'
export default function Telephony_NetStack_HttpFetchTest() { export default function Telephony_NetStack_HttpFetchTest() {
...@@ -26,31 +27,34 @@ export default function Telephony_NetStack_HttpFetchTest() { ...@@ -26,31 +27,34 @@ export default function Telephony_NetStack_HttpFetchTest() {
* @tc.desc function test * @tc.desc function test
*/ */
it("Telephony_NetStack_HttpTestFetchBasicGet_0100", 0, function (done) { it("Telephony_NetStack_HttpTestFetchBasicGet_0100", 0, function (done) {
fetchTest(); let httpRequest = http.createHttp();
var objectTimeOut = setTimeout(function timeout() { httpRequest.request("https://httpbin.org", {
fetchTest(); readTimeout: 10000,
}, 30000); connectTimeout: 10000
}, (err, data) => {
function fetchTest() { if (!err) {
fetch.fetch({ fetch.fetch({
url: "https://httpbin.org/anything", url: "https://httpbin.org/anything",
success: function (data) { success: function (data) {
console.info("NetStack fetch success " + JSON.stringify(data)); console.info("NetStack fetch success " + JSON.stringify(data));
expect(data.code === 200 || data.code > 500).assertTrue(); expect(data.code === 200 || data.code > 500).assertTrue();
clearTimeout(objectTimeOut);
done(); done();
}, },
fail: function () { fail: function () {
console.info("NetStack Fetch Failed"); console.info("NetStack Fetch Failed");
expect().assertFail(); expect().assertFail();
clearTimeout(objectTimeOut);
done(); done();
}, },
complete: function () { complete: function () {
console.info("NetStack Fetch Complete"); console.info("NetStack Fetch Complete");
} }
}) })
} else {
console.info('error:' + JSON.stringify(err));
done();
} }
httpRequest.destroy();
});
}); });
/** /**
...@@ -59,12 +63,12 @@ export default function Telephony_NetStack_HttpFetchTest() { ...@@ -59,12 +63,12 @@ export default function Telephony_NetStack_HttpFetchTest() {
* @tc.desc function test * @tc.desc function test
*/ */
it("Telephony_NetStack_HttpTestFetchUserAgent_0100", 0, function (done) { it("Telephony_NetStack_HttpTestFetchUserAgent_0100", 0, function (done) {
fetchTest(); let httpRequest = http.createHttp();
var objectTimeOut = setTimeout(function timeout() { httpRequest.request("https://httpbin.org", {
fetchTest(); readTimeout: 10000,
}, 30000); connectTimeout: 10000
}, (err, data) => {
function fetchTest() { if (!err) {
fetch.fetch({ fetch.fetch({
url: "https://httpbin.org/user-agent", url: "https://httpbin.org/user-agent",
success: function (data) { success: function (data) {
...@@ -73,20 +77,23 @@ export default function Telephony_NetStack_HttpFetchTest() { ...@@ -73,20 +77,23 @@ export default function Telephony_NetStack_HttpFetchTest() {
if (data.code === 200) { if (data.code === 200) {
expect(JSON.parse(data.data.toString())["user-agent"] == "libcurl-agent/1.0").assertTrue(); expect(JSON.parse(data.data.toString())["user-agent"] == "libcurl-agent/1.0").assertTrue();
} }
clearTimeout(objectTimeOut);
done(); done();
}, },
fail: function () { fail: function () {
console.info("NetStack Fetch Failed"); console.info("NetStack Fetch Failed");
expect().assertFail(); expect().assertFail();
clearTimeout(objectTimeOut);
done(); done();
}, },
complete: function () { complete: function () {
console.info("NetStack fetch complete"); console.info("NetStack fetch complete");
} }
}) })
} else {
console.info('error:' + JSON.stringify(err));
done();
} }
httpRequest.destroy();
});
}); });
/** /**
...@@ -95,12 +102,12 @@ export default function Telephony_NetStack_HttpFetchTest() { ...@@ -95,12 +102,12 @@ export default function Telephony_NetStack_HttpFetchTest() {
* @tc.desc function test * @tc.desc function test
*/ */
it("Telephony_NetStack_HttpTestFetchUserAgent_0200", 0, function (done) { it("Telephony_NetStack_HttpTestFetchUserAgent_0200", 0, function (done) {
fetchTest(); let httpRequest = http.createHttp();
var objectTimeOut = setTimeout(function timeout() { httpRequest.request("https://httpbin.org", {
fetchTest(); readTimeout: 10000,
}, 30000); connectTimeout: 10000
}, (err, data) => {
function fetchTest() { if (!err) {
fetch.fetch({ fetch.fetch({
url: "https://httpbin.org/user-agent", url: "https://httpbin.org/user-agent",
responseType: "json", responseType: "json",
...@@ -110,20 +117,23 @@ export default function Telephony_NetStack_HttpFetchTest() { ...@@ -110,20 +117,23 @@ export default function Telephony_NetStack_HttpFetchTest() {
if (data.code === 200) { if (data.code === 200) {
expect(data.data["user-agent"] == "libcurl-agent/1.0").assertTrue(); expect(data.data["user-agent"] == "libcurl-agent/1.0").assertTrue();
} }
clearTimeout(objectTimeOut);
done(); done();
}, },
fail: function () { fail: function () {
console.info("NetStack Fetch Failed"); console.info("NetStack Fetch Failed");
expect().assertFail(); expect().assertFail();
clearTimeout(objectTimeOut);
done(); done();
}, },
complete: function () { complete: function () {
console.info("NetStack fetch complete"); console.info("NetStack fetch complete");
} }
}) })
} else {
console.info('error:' + JSON.stringify(err));
done();
} }
httpRequest.destroy();
});
}); });
/** /**
...@@ -132,12 +142,12 @@ export default function Telephony_NetStack_HttpFetchTest() { ...@@ -132,12 +142,12 @@ export default function Telephony_NetStack_HttpFetchTest() {
* @tc.desc function test * @tc.desc function test
*/ */
it("Telephony_NetStack_HttpTestFetchHeader_0100", 0, function (done) { it("Telephony_NetStack_HttpTestFetchHeader_0100", 0, function (done) {
fetchTest(); let httpRequest = http.createHttp();
var objectTimeOut = setTimeout(function timeout() { httpRequest.request("https://httpbin.org", {
fetchTest(); readTimeout: 10000,
}, 30000); connectTimeout: 10000
}, (err, data) => {
function fetchTest() { if (!err) {
fetch.fetch({ fetch.fetch({
url: "https://httpbin.org/headers", url: "https://httpbin.org/headers",
success: function (data) { success: function (data) {
...@@ -146,20 +156,23 @@ export default function Telephony_NetStack_HttpFetchTest() { ...@@ -146,20 +156,23 @@ export default function Telephony_NetStack_HttpFetchTest() {
if (data.code === 200) { if (data.code === 200) {
expect(JSON.parse(data.data.toString())["headers"]["Content-Type"] === "application/json").assertTrue(); expect(JSON.parse(data.data.toString())["headers"]["Content-Type"] === "application/json").assertTrue();
} }
clearTimeout(objectTimeOut);
done(); done();
}, },
fail: function () { fail: function () {
console.info("NetStack Fetch Failed"); console.info("NetStack Fetch Failed");
expect().assertFail(); expect().assertFail();
clearTimeout(objectTimeOut);
done(); done();
}, },
complete: function () { complete: function () {
console.info("NetStack Fetch Complete"); console.info("NetStack Fetch Complete");
} }
}) })
} else {
console.info('error:' + JSON.stringify(err));
done();
} }
httpRequest.destroy();
});
}); });
/** /**
...@@ -168,12 +181,12 @@ export default function Telephony_NetStack_HttpFetchTest() { ...@@ -168,12 +181,12 @@ export default function Telephony_NetStack_HttpFetchTest() {
* @tc.desc function test * @tc.desc function test
*/ */
it("Telephony_NetStack_HttpTestFetchHeader_0200", 0, function (done) { it("Telephony_NetStack_HttpTestFetchHeader_0200", 0, function (done) {
fetchTest(); let httpRequest = http.createHttp();
var objectTimeOut = setTimeout(function timeout() { httpRequest.request("https://httpbin.org", {
fetchTest(); readTimeout: 10000,
}, 30000); connectTimeout: 10000
}, (err, data) => {
function fetchTest() { if (!err) {
fetch.fetch({ fetch.fetch({
url: "https://httpbin.org/headers", url: "https://httpbin.org/headers",
responseType: "json", responseType: "json",
...@@ -183,20 +196,23 @@ export default function Telephony_NetStack_HttpFetchTest() { ...@@ -183,20 +196,23 @@ export default function Telephony_NetStack_HttpFetchTest() {
if (data.code === 200) { if (data.code === 200) {
expect(data.data["headers"]["Content-Type"] === "application/json").assertTrue(); expect(data.data["headers"]["Content-Type"] === "application/json").assertTrue();
} }
clearTimeout(objectTimeOut);
done(); done();
}, },
fail: function () { fail: function () {
console.info("NetStack Fetch Failed"); console.info("NetStack Fetch Failed");
expect().assertFail(); expect().assertFail();
clearTimeout(objectTimeOut);
done(); done();
}, },
complete: function () { complete: function () {
console.info("NetStack Fetch Complete"); console.info("NetStack Fetch Complete");
} }
}) })
} else {
console.info('error:' + JSON.stringify(err));
done();
} }
httpRequest.destroy();
});
}); });
/** /**
...@@ -205,12 +221,12 @@ export default function Telephony_NetStack_HttpFetchTest() { ...@@ -205,12 +221,12 @@ export default function Telephony_NetStack_HttpFetchTest() {
* @tc.desc function test * @tc.desc function test
*/ */
it("Telephony_NetStack_HttpTestFetchPost_0100", 0, function (done) { it("Telephony_NetStack_HttpTestFetchPost_0100", 0, function (done) {
fetchTest(); let httpRequest = http.createHttp();
var objectTimeOut = setTimeout(function timeout() { httpRequest.request("https://httpbin.org", {
fetchTest(); readTimeout: 10000,
}, 30000); connectTimeout: 10000
}, (err, data) => {
function fetchTest() { if (!err) {
fetch.fetch({ fetch.fetch({
url: "https://httpbin.org/post", url: "https://httpbin.org/post",
method: "POST", method: "POST",
...@@ -221,20 +237,23 @@ export default function Telephony_NetStack_HttpFetchTest() { ...@@ -221,20 +237,23 @@ export default function Telephony_NetStack_HttpFetchTest() {
if (data.code === 200) { if (data.code === 200) {
expect(JSON.parse(data.data.toString())["data"] === "MineMine").assertTrue(); expect(JSON.parse(data.data.toString())["data"] === "MineMine").assertTrue();
} }
clearTimeout(objectTimeOut);
done(); done();
}, },
fail: function () { fail: function () {
console.info("NetStack Fetch Failed"); console.info("NetStack Fetch Failed");
expect().assertFail(); expect().assertFail();
clearTimeout(objectTimeOut);
done() done()
}, },
complete: function () { complete: function () {
console.info("NetStack Fetch Complete"); console.info("NetStack Fetch Complete");
} }
}) })
} else {
console.info('error:' + JSON.stringify(err));
done();
} }
httpRequest.destroy();
});
}); });
/** /**
...@@ -243,12 +262,12 @@ export default function Telephony_NetStack_HttpFetchTest() { ...@@ -243,12 +262,12 @@ export default function Telephony_NetStack_HttpFetchTest() {
* @tc.desc function test * @tc.desc function test
*/ */
it("Telephony_NetStack_HttpTestFetchPost_0200", 0, function (done) { it("Telephony_NetStack_HttpTestFetchPost_0200", 0, function (done) {
fetchTest(); let httpRequest = http.createHttp();
var objectTimeOut = setTimeout(function timeout() { httpRequest.request("https://httpbin.org", {
fetchTest(); readTimeout: 10000,
}, 30000); connectTimeout: 10000
}, (err, data) => {
function fetchTest() { if (!err) {
fetch.fetch({ fetch.fetch({
url: "https://httpbin.org/post", url: "https://httpbin.org/post",
method: "POST", method: "POST",
...@@ -260,20 +279,23 @@ export default function Telephony_NetStack_HttpFetchTest() { ...@@ -260,20 +279,23 @@ export default function Telephony_NetStack_HttpFetchTest() {
if (data.code === 200) { if (data.code === 200) {
expect(data.data["data"] === "MineMine").assertTrue(); expect(data.data["data"] === "MineMine").assertTrue();
} }
clearTimeout(objectTimeOut);
done(); done();
}, },
fail: function () { fail: function () {
console.info("NetStack Fetch Failed"); console.info("NetStack Fetch Failed");
expect().assertFail(); expect().assertFail();
clearTimeout(objectTimeOut);
done(); done();
}, },
complete: function () { complete: function () {
console.info("NetStack Fetch Complete"); console.info("NetStack Fetch Complete");
} }
}) })
} else {
console.info('error:' + JSON.stringify(err));
done();
} }
httpRequest.destroy();
});
}); });
/** /**
...@@ -282,12 +304,12 @@ export default function Telephony_NetStack_HttpFetchTest() { ...@@ -282,12 +304,12 @@ export default function Telephony_NetStack_HttpFetchTest() {
* @tc.desc function test * @tc.desc function test
*/ */
it("Telephony_NetStack_HttpTestFetchPut_0100", 0, function (done) { it("Telephony_NetStack_HttpTestFetchPut_0100", 0, function (done) {
fetchTest(); let httpRequest = http.createHttp();
var objectTimeOut = setTimeout(function timeout() { httpRequest.request("https://httpbin.org", {
fetchTest(); readTimeout: 10000,
}, 30000); connectTimeout: 10000
}, (err, data) => {
function fetchTest() { if (!err) {
fetch.fetch({ fetch.fetch({
url: "https://httpbin.org/put", url: "https://httpbin.org/put",
method: "PUT", method: "PUT",
...@@ -298,20 +320,23 @@ export default function Telephony_NetStack_HttpFetchTest() { ...@@ -298,20 +320,23 @@ export default function Telephony_NetStack_HttpFetchTest() {
if (data.code === 200) { if (data.code === 200) {
expect(JSON.parse(data.data.toString())["data"] === "MineMine").assertTrue(); expect(JSON.parse(data.data.toString())["data"] === "MineMine").assertTrue();
} }
clearTimeout(objectTimeOut);
done(); done();
}, },
fail: function () { fail: function () {
console.info("NetStack Fetch Failed"); console.info("NetStack Fetch Failed");
expect().assertFail(); expect().assertFail();
clearTimeout(objectTimeOut);
done(); done();
}, },
complete: function () { complete: function () {
console.info("NetStack Fetch Complete"); console.info("NetStack Fetch Complete");
} }
}) })
} else {
console.info('error:' + JSON.stringify(err));
done();
} }
httpRequest.destroy();
});
}); });
/** /**
...@@ -320,12 +345,12 @@ export default function Telephony_NetStack_HttpFetchTest() { ...@@ -320,12 +345,12 @@ export default function Telephony_NetStack_HttpFetchTest() {
* @tc.desc function test * @tc.desc function test
*/ */
it("Telephony_NetStack_HttpTestFetchPut_0200", 0, function (done) { it("Telephony_NetStack_HttpTestFetchPut_0200", 0, function (done) {
fetchTest(); let httpRequest = http.createHttp();
var objectTimeOut = setTimeout(function timeout() { httpRequest.request("https://httpbin.org", {
fetchTest(); readTimeout: 10000,
}, 30000); connectTimeout: 10000
}, (err, data) => {
function fetchTest() { if (!err) {
fetch.fetch({ fetch.fetch({
url: "https://httpbin.org/put", url: "https://httpbin.org/put",
method: "PUT", method: "PUT",
...@@ -337,20 +362,23 @@ export default function Telephony_NetStack_HttpFetchTest() { ...@@ -337,20 +362,23 @@ export default function Telephony_NetStack_HttpFetchTest() {
expect(data.code === 200 || data.code > 500).assertTrue(); expect(data.code === 200 || data.code > 500).assertTrue();
expect(data.data["data"] === "MineMine").assertTrue(); expect(data.data["data"] === "MineMine").assertTrue();
} }
clearTimeout(objectTimeOut);
done(); done();
}, },
fail: function () { fail: function () {
console.info("NetStack Fetch Failed"); console.info("NetStack Fetch Failed");
expect().assertFail(); expect().assertFail();
clearTimeout(objectTimeOut);
done(); done();
}, },
complete: function () { complete: function () {
console.info("NetStack Fetch Complete"); console.info("NetStack Fetch Complete");
} }
}) })
} else {
console.info('error:' + JSON.stringify(err));
done();
} }
httpRequest.destroy();
});
}); });
/** /**
...@@ -359,12 +387,12 @@ export default function Telephony_NetStack_HttpFetchTest() { ...@@ -359,12 +387,12 @@ export default function Telephony_NetStack_HttpFetchTest() {
* @tc.desc function test * @tc.desc function test
*/ */
it("Telephony_NetStack_HttpTestFetchUrlEncode_0100", 0, function (done) { it("Telephony_NetStack_HttpTestFetchUrlEncode_0100", 0, function (done) {
fetchTest(); let httpRequest = http.createHttp();
var objectTimeOut = setTimeout(function timeout() { httpRequest.request("https://httpbin.org", {
fetchTest(); readTimeout: 10000,
}, 30000); connectTimeout: 10000
}, (err, data) => {
function fetchTest() { if (!err) {
fetch.fetch({ fetch.fetch({
url: "https://httpbin.org/response-headers?key1=val1&key2=val2", url: "https://httpbin.org/response-headers?key1=val1&key2=val2",
data: { data: {
...@@ -380,20 +408,23 @@ export default function Telephony_NetStack_HttpFetchTest() { ...@@ -380,20 +408,23 @@ export default function Telephony_NetStack_HttpFetchTest() {
expect(JSON.parse(data.data.toString())["name1"] === "mine1").assertTrue(); expect(JSON.parse(data.data.toString())["name1"] === "mine1").assertTrue();
expect(JSON.parse(data.data.toString())["name2"] === "mine2").assertTrue(); expect(JSON.parse(data.data.toString())["name2"] === "mine2").assertTrue();
} }
clearTimeout(objectTimeOut);
done(); done();
}, },
fail: function () { fail: function () {
console.info("NetStack Fetch Failed"); console.info("NetStack Fetch Failed");
expect().assertFail(); expect().assertFail();
clearTimeout(objectTimeOut);
done(); done();
}, },
complete: function () { complete: function () {
console.info("NetStack Fetch Complete"); console.info("NetStack Fetch Complete");
} }
}) })
} else {
console.info('error:' + JSON.stringify(err));
done();
} }
httpRequest.destroy();
});
}); });
/** /**
...@@ -402,12 +433,12 @@ export default function Telephony_NetStack_HttpFetchTest() { ...@@ -402,12 +433,12 @@ export default function Telephony_NetStack_HttpFetchTest() {
* @tc.desc function test * @tc.desc function test
*/ */
it("Telephony_NetStack_HttpTestFetchUrlEncode_0200", 0, function (done) { it("Telephony_NetStack_HttpTestFetchUrlEncode_0200", 0, function (done) {
fetchTest(); let httpRequest = http.createHttp();
var objectTimeOut = setTimeout(function timeout() { httpRequest.request("https://httpbin.org", {
fetchTest(); readTimeout: 10000,
}, 30000); connectTimeout: 10000
}, (err, data) => {
function fetchTest() { if (!err) {
fetch.fetch({ fetch.fetch({
url: "https://httpbin.org/response-headers?key1=val1&key2=val2", url: "https://httpbin.org/response-headers?key1=val1&key2=val2",
data: { data: {
...@@ -424,20 +455,23 @@ export default function Telephony_NetStack_HttpFetchTest() { ...@@ -424,20 +455,23 @@ export default function Telephony_NetStack_HttpFetchTest() {
expect(data.data["name1"] === "mine1").assertTrue(); expect(data.data["name1"] === "mine1").assertTrue();
expect(data.data["name2"] === "mine2").assertTrue(); expect(data.data["name2"] === "mine2").assertTrue();
} }
clearTimeout(objectTimeOut);
done(); done();
}, },
fail: function () { fail: function () {
console.info("NetStack Fetch Failed"); console.info("NetStack Fetch Failed");
expect().assertFail(); expect().assertFail();
clearTimeout(objectTimeOut);
done(); done();
}, },
complete: function () { complete: function () {
console.info("NetStack Fetch Complete"); console.info("NetStack Fetch Complete");
} }
}) })
} else {
console.info('error:' + JSON.stringify(err));
done();
} }
httpRequest.destroy();
});
}); });
}); });
} }
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册