From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on gnuweeb.org X-Spam-Level: X-Spam-Status: No, score=-0.8 required=5.0 tests=ALL_TRUSTED,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,NO_DNS_FOR_FROM, NUMERIC_HTTP_ADDR,URIBL_BLOCKED,WEIRD_PORT autolearn=no autolearn_force=no version=3.4.6 Received: from localhost.localdomain (unknown [180.246.144.41]) by gnuweeb.org (Postfix) with ESMTPSA id 06A7180A13; Sun, 21 Aug 2022 11:25:35 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=gnuweeb.org; s=default; t=1661081137; bh=AKz+XSmkJtLOR/ydAJECpST70kJIA3i4uioQuPB8tQs=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=WuWIgBVSbQ6hOoWgqA3AohELizIzo/Xl7Hn1y33Z3UZO3ZgwpeAkYeY7MDfUDl9Kj O8t2krs58Jkw2HQ7X6MvytjCgWOyFhllUQVqqCkUSMEwxZjyCFKOTIDLfZ2qCyjO3q /kmDFBB/ihvFzM11uJm/yG+S4TkC2j2VTKq7HaK/p1dnfWwsoCp95Lq6wMI957d9bn tZIJ62yjpxYps759v50n/2jc7R6JpabFESjOhm9VP9Nazjb7pZU4ZBsXaYn35v3i3i LU8rksH1Yb6o9iwsxQ/t1Ou1aGbIBczmif1VL3HR0CBZWPb9LSeSUc4mRpHohAIBx8 pFVk8k+HgHg9Q== From: Ammar Faizi To: Alviro Iskandar Setiawan Cc: Ammar Faizi , Muhammad Rizki , Kanna Scarlet , GNU/Weeb Mailing List Subject: [PATCH v1 09/22] tests/js/ring: Add simple HTTP POST request example in NodeJS Date: Sun, 21 Aug 2022 18:24:40 +0700 Message-Id: <20220821112453.3026255-10-ammarfaizi2@gnuweeb.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20220821112453.3026255-1-ammarfaizi2@gnuweeb.org> References: <20220821112453.3026255-1-ammarfaizi2@gnuweeb.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit List-Id: Signed-off-by: Ammar Faizi --- tests/js/ring.js | 50 +++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 49 insertions(+), 1 deletion(-) diff --git a/tests/js/ring.js b/tests/js/ring.js index de4baaf..3a4d25d 100644 --- a/tests/js/ring.js +++ b/tests/js/ring.js @@ -87,16 +87,64 @@ function test_chnet_ring_multiple() j = ring.for_each_cqe(function (cqe) { let ch = cqe.user_data; assert(cqe.res == 13); - assert(ch.read_buf()); + assert(ch.read_buf() === "Hello World!\n"); }); assert(j == i); ring.cq_advance(j); } +class SimpleHttp { + constructor(url, method = "GET") { + this.ring = chnet.create_ring(1); + this.ch = chnet.create_net(); + this.ch.set_url(url); + this.ch.set_method(method); + this.res = -1; + } + + prep_read(read_size) { + let sqe = this.ring.get_sqe(); + sqe.prep_read(this.ch, read_size); + sqe.set_user_data(this); + } + + set_payload(payload) { + this.ch.set_payload(payload); + } + + run() { + this.ring.submit_sqe(); + this.ring.wait_cqe(1); + this.ring.for_each_cqe(function (cqe) { + let that = cqe.user_data; + that.res = cqe.res; + if (cqe.res < 0) + console.log("Error: "+that.ch.get_error()); + }); + this.ring.cq_advance(1); + } + + get_buffer() { + return this.ch.read_buf(); + } +} + +function test_simple_http() +{ + const payload = "Hello World! AAAAAAAAAAAAAAAAAAAAAAAAA\n"; + let h = new SimpleHttp("http://127.0.0.1:8000/index.php?action=body", "POST"); + h.set_payload(payload); + h.prep_read(1024); + h.run(); + assert(h.get_buffer() === payload); + assert(h.ch.read_ret() === payload.length); +} + function main() { test_nop(); test_chnet_ring_multiple(); + test_simple_http(); } main(); -- Ammar Faizi