github.com/google/go-safeweb@v0.0.0-20231219055052-64d8cfc90fbb/safehttp/safehttptest/request.go (about) 1 // Copyright 2020 Google LLC 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // https://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package safehttptest 16 17 import ( 18 "io" 19 "net/http/httptest" 20 21 "github.com/google/go-safeweb/safehttp" 22 ) 23 24 // NewRequest returns a new incoming server Request, 25 // suitable for passing to an http.Handler for testing. 26 // 27 // The target is the RFC 7230 "request-target": it may 28 // be either a path or an absolute URL. If target is an 29 // absolute URL, the host name from the URL is used. 30 // Otherwise, "example.com" is used. 31 // 32 // The TLS field is set to a non-nil dummy value if 33 // target has scheme "https". 34 // 35 // The Request.Proto is always HTTP/1.1. 36 // 37 // An empty method means "GET". 38 // 39 // The provided body may be nil. If the body is of type 40 // *bytes.Reader, *strings.Reader, or *bytes.Buffer, the 41 // Request.ContentLength is set. 42 // 43 // NewRequest panics on error for ease of use in testing, 44 // where a panic is acceptable. 45 // 46 // To generate a client HTTP request instead of a server 47 // request, see the NewRequest function in the net/http 48 // package. 49 func NewRequest(method, target string, body io.Reader) *safehttp.IncomingRequest { 50 req := httptest.NewRequest(method, target, body) 51 return safehttp.NewIncomingRequest(req) 52 }