github.com/hellofresh/janus@v0.0.0-20230925145208-ce8de8183c67/pkg/proxy/reverse_proxy_test.go (about)

     1  package proxy
     2  
     3  import (
     4  	"github.com/stretchr/testify/assert"
     5  	"net/http"
     6  	"testing"
     7  )
     8  
     9  func newTestRequest() *http.Request {
    10  	return &http.Request{
    11  		Method:           "",
    12  		URL:              nil,
    13  		Proto:            "",
    14  		ProtoMajor:       0,
    15  		ProtoMinor:       0,
    16  		Header:           nil,
    17  		Body:             nil,
    18  		GetBody:          nil,
    19  		ContentLength:    0,
    20  		TransferEncoding: nil,
    21  		Close:            false,
    22  		Host:             "",
    23  		Form:             nil,
    24  		PostForm:         nil,
    25  		MultipartForm:    nil,
    26  		Trailer:          nil,
    27  		RemoteAddr:       "",
    28  		RequestURI:       "",
    29  		TLS:              nil,
    30  		Cancel:           nil,
    31  		Response:         nil,
    32  	}
    33  }
    34  func TestStripPathWithParams(t *testing.T) {
    35  	t.Run("properly strips path - params and listenPath", func(t *testing.T) {
    36  		req := newTestRequest()
    37  		path := "/prepath/my-service/endpoint"
    38  		listenPath := "/prepath/{service}/*"
    39  		paramNames := []string{"service"}
    40  
    41  		old := chiURLParam
    42  		defer func() {chiURLParam = old}()
    43  
    44  		chiURLParam = func(r *http.Request, key string) string {
    45  			return "my-service"
    46  		}
    47  		returnPath := stripPathWithParams(req, path, listenPath, paramNames)
    48  
    49  		assert.Equal(t, "/endpoint", returnPath)
    50  	})
    51  
    52  	t.Run("check that strip logic is correct if value is not in path", func(t *testing.T) {
    53  		req := newTestRequest()
    54  		path := "/prepath/my-service/endpoint"
    55  		listenPath := "/prepath/{service}/*"
    56  		paramNames := []string{"service"}
    57  
    58  		old := chiURLParam
    59  		defer func() {chiURLParam = old}()
    60  
    61  		chiURLParam = func(r *http.Request, key string) string {
    62  			return "other-value"
    63  		}
    64  		returnPath := stripPathWithParams(req, path, listenPath, paramNames)
    65  
    66  		assert.Equal(t, "/my-service/endpoint", returnPath)
    67  	})
    68  }