From patchwork Thu Jan 20 13:08:47 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: M X-Patchwork-Id: 36652 Return-Path: X-Original-To: patchwork@mira.cbaines.net Delivered-To: patchwork@mira.cbaines.net Received: by mira.cbaines.net (Postfix, from userid 113) id 59C2027BBEA; Thu, 20 Jan 2022 19:46:34 +0000 (GMT) X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on mira.cbaines.net X-Spam-Level: X-Spam-Status: No, score=-2.7 required=5.0 tests=BAYES_00,DKIM_INVALID, DKIM_SIGNED,FREEMAIL_FROM,MAILING_LIST_MULTI,RCVD_IN_MSPIKE_H3, RCVD_IN_MSPIKE_WL,SPF_HELO_PASS,URIBL_BLOCKED autolearn=unavailable autolearn_force=no version=3.4.6 Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mira.cbaines.net (Postfix) with ESMTPS id B60FE27BBE9 for ; Thu, 20 Jan 2022 19:46:33 +0000 (GMT) Received: from localhost ([::1]:56758 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1nAdOC-00029O-Pg for patchwork@mira.cbaines.net; Thu, 20 Jan 2022 14:46:32 -0500 Received: from eggs.gnu.org ([209.51.188.92]:50802) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1nAXCX-0004EH-8r for guix-patches@gnu.org; Thu, 20 Jan 2022 08:10:06 -0500 Received: from debbugs.gnu.org ([209.51.188.43]:35574) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1nAXCW-0006T3-6g for guix-patches@gnu.org; Thu, 20 Jan 2022 08:10:04 -0500 Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1nAXCW-0004tH-0k for guix-patches@gnu.org; Thu, 20 Jan 2022 08:10:04 -0500 X-Loop: help-debbugs@gnu.org Subject: [bug#53389] [PATCH 7/9] tests: Allow checking the URI of a HTTP request. Resent-From: Maxime Devos Original-Sender: "Debbugs-submit" Resent-CC: guix-patches@gnu.org Resent-Date: Thu, 20 Jan 2022 13:10:03 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 53389 X-GNU-PR-Package: guix-patches X-GNU-PR-Keywords: patch To: 53389@debbugs.gnu.org Cc: ludo@gnu.org, Maxime Devos Received: via spool by 53389-submit@debbugs.gnu.org id=B53389.164268415918695 (code B ref 53389); Thu, 20 Jan 2022 13:10:03 +0000 Received: (at 53389) by debbugs.gnu.org; 20 Jan 2022 13:09:19 +0000 Received: from localhost ([127.0.0.1]:56702 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1nAXBm-0004rM-O7 for submit@debbugs.gnu.org; Thu, 20 Jan 2022 08:09:19 -0500 Received: from baptiste.telenet-ops.be ([195.130.132.51]:42074) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1nAXBM-0004oy-Kt for 53389@debbugs.gnu.org; Thu, 20 Jan 2022 08:08:54 -0500 Received: from localhost.localdomain ([IPv6:2a02:1811:8c09:9d00:3c5f:2eff:feb0:ba5a]) by baptiste.telenet-ops.be with bizsmtp id l18q2600H4UW6Th0118rNy; Thu, 20 Jan 2022 14:08:52 +0100 From: Maxime Devos Date: Thu, 20 Jan 2022 13:08:47 +0000 Message-Id: <20220120130849.292178-7-maximedevos@telenet.be> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20220120130849.292178-1-maximedevos@telenet.be> References: <20220120130849.292178-1-maximedevos@telenet.be> MIME-Version: 1.0 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=telenet.be; s=r22; t=1642684132; bh=qXHokLrX3UhbhK5gTp4pjeibyVHhQkw8mgeu4FprdvI=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=iLbuSuI+tlgqAs2dSPW9TEL1ZAVxHeXW6ERMQ25+EMz0xNLPqaHzwunTKHbbVkUQZ 9YlY7WhcCTBxuLqm3TE31LjXtn4JePGVnbyOp3Nmry7b87ykxSL2xhPtpi1LN//WzA 0kL+xoMUJDjUXx/dqg/46PdDJkeiDzDrODUzfJoYFZDM5lvYeOJ45ZK9tAYpgBUG4s kCzOqEWH0DM4Xyb+RwtuOonBLr0yzNtYv6L+7YrEj78sCNqGE+uNgAAOHpniB3NZy8 GrGWOOi/QvCYdl7cdl9wTimzDIrNC3PDt+DFOB3lS3fdSo76MiR/sGX5r9/UxAgNuH Nyl5OHZWLtQSQ== X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list X-BeenThere: guix-patches@gnu.org List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: guix-patches-bounces+patchwork=mira.cbaines.net@gnu.org Sender: "Guix-patches" X-getmail-retrieved-from-mailbox: Patches * guix/tests/http.scm (call-with-http-server): Thunk RESPONSES+DATA. [maybe-uri?]: New procedure. [sanitize-response+data]: New procedure extracted from the definition of 'responses', support triples containing a URI. [responses]: Thunk. [handle]: Verify the URI. Adjust for thunking. (with-http-server): Adjust for thunking. --- guix/tests/http.scm | 35 +++++++++++++++++++++++++---------- 1 file changed, 25 insertions(+), 10 deletions(-) diff --git a/guix/tests/http.scm b/guix/tests/http.scm index 2f65df4029..6af1948211 100644 --- a/guix/tests/http.scm +++ b/guix/tests/http.scm @@ -21,7 +21,9 @@ #:use-module (ice-9 threads) #:use-module (web server) #:use-module (web server http) + #:use-module (web request) #:use-module (web response) + #:use-module (web uri) #:use-module (srfi srfi-11) #:use-module (srfi srfi-39) #:use-module (ice-9 match) @@ -144,7 +146,10 @@ It will also quit if LAST-RESPONSE? returns true." (define* (call-with-http-server responses+data thunk #:key (keep-lingering? #false)) "Call THUNK with an HTTP server running and returning RESPONSES+DATA on HTTP requests. Each element of RESPONSES+DATA must be a tuple containing a -response and a string, or an HTTP response code and a string. +response and a string, or an HTTP response code and a string. Alternatively, +the elements can triples with the URI path (including the query, if any) +as the first part of the triple. In that case, the request URI is verified +against the URI in RESPONSES+DATA. The argument RESPONSES+DATA is thunked. As such, RESPONSES+DATA can use %http-server-port. %http-server-port will be set to the port listened at. @@ -152,18 +157,28 @@ It will be set for the dynamic extent of THUNK and RESPONSES+DATA. The server will exit after the last response. When KEEP-LINGERING? is false, the server will also exit after THUNK returns." + (define (maybe-uri? object) + (or (string? object) (eq? object 'any))) + (define (sanitize-response+data response+data) + (match response+data + ((response data) + (sanitize-response+data (list 'any response data))) + (((? maybe-uri? uri) (? response? response) data) + (list uri response data)) + (((? maybe-uri? uri) (? integer? code) data) + (list uri + (build-response #:code code + #:reason-phrase "Such is life") + data)))) (define (responses) - (map (match-lambda - (((? response? response) data) - (list response data)) - (((? integer? code) data) - (list (build-response #:code code - #:reason-phrase "Such is life") - data))) - (responses+data))) + (map sanitize-response+data (responses+data))) (define (handle request body) (match (responses) - (((response data) rest ...) + (((uri response data) rest ...) + (unless (or (eq? uri 'any) + (string=? uri (uri->string (request-uri request)))) + (error "this URI should not be contacted!" + (request-uri request))) (set! responses (const rest)) (values response data)))) (call-with-http-server* handle thunk #:keep-lingering? keep-lingering?