go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/server/auth/authtest/request.go (about)

     1  // Copyright 2023 The LUCI Authors.
     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  //      http://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 authtest
    16  
    17  import (
    18  	"net/http"
    19  )
    20  
    21  // FakeRequestMetadata implements RequestMetadata using fake values.
    22  type FakeRequestMetadata struct {
    23  	FakeHeader     http.Header
    24  	FakeCookies    map[string]*http.Cookie
    25  	FakeRemoteAddr string
    26  	FakeHost       string
    27  }
    28  
    29  // NewFakeRequestMetadata constructs an empty FakeRequestMetadata.
    30  func NewFakeRequestMetadata() *FakeRequestMetadata {
    31  	return &FakeRequestMetadata{
    32  		FakeHeader:     map[string][]string{},
    33  		FakeCookies:    map[string]*http.Cookie{},
    34  		FakeRemoteAddr: "127.0.0.1",
    35  		FakeHost:       "fake.example.com",
    36  	}
    37  }
    38  
    39  // Header is part of RequestMetadata interface.
    40  func (r *FakeRequestMetadata) Header(key string) string {
    41  	return r.FakeHeader.Get(key)
    42  }
    43  
    44  // Cookie is part of RequestMetadata interface.
    45  func (r *FakeRequestMetadata) Cookie(key string) (*http.Cookie, error) {
    46  	if c, ok := r.FakeCookies[key]; ok {
    47  		return c, nil
    48  	}
    49  	return nil, http.ErrNoCookie
    50  }
    51  
    52  // RemoteAddr is part of RequestMetadata interface.
    53  func (r *FakeRequestMetadata) RemoteAddr() string {
    54  	return r.FakeRemoteAddr
    55  }
    56  
    57  // Host is part of RequestMetadata interface.
    58  func (r *FakeRequestMetadata) Host() string {
    59  	return r.FakeHost
    60  }