Skip to content

Commit

Permalink
server: test proxy server
Browse files Browse the repository at this point in the history
Updates the proxy server inline tests to test the server rather than the
HTTP proxy handler.
  • Loading branch information
andydunstall committed Jul 14, 2024
1 parent 099b50e commit ed6b423
Show file tree
Hide file tree
Showing 5 changed files with 473 additions and 482 deletions.
37 changes: 0 additions & 37 deletions server/proxy/httpproxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"net"
"net/http"
"net/http/httputil"
"strings"
"time"

"go.uber.org/zap"
Expand Down Expand Up @@ -149,39 +148,3 @@ func errorResponse(w http.ResponseWriter, statusCode int, message string) error
}
return json.NewEncoder(w).Encode(m)
}

// EndpointIDFromRequest returns the endpoint ID from the HTTP request, or an
// empty string if no endpoint ID is specified.
//
// This will check both the 'x-piko-endpoint' header and 'Host' header, where
// x-piko-endpoint takes precedence.
func EndpointIDFromRequest(r *http.Request) string {
endpointID := r.Header.Get("x-piko-endpoint")
if endpointID != "" {
return endpointID
}

// Strip the port if given.
host, _, err := net.SplitHostPort(r.Host)
if err != nil {
host = r.Host
}

if host == "" {
return ""
}
if net.ParseIP(host) != nil {
// Ignore IP addresses.
return ""
}
if strings.Contains(host, ".") {
// If a host is given and contains a separator, use the bottom-level
// domain as the endpoint ID.
//
// Such as if the domain is 'xyz.piko.example.com', then 'xyz' is the
// endpoint ID.
return strings.Split(host, ".")[0]
}

return ""
}
295 changes: 0 additions & 295 deletions server/proxy/httpproxy_test.go

This file was deleted.

37 changes: 37 additions & 0 deletions server/proxy/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"
"net"
"net/http"
"strings"

"github.com/gin-gonic/gin"
"github.com/prometheus/client_golang/prometheus"
Expand Down Expand Up @@ -124,6 +125,42 @@ func (s *Server) panicRoute(c *gin.Context, err any) {
c.AbortWithStatus(http.StatusInternalServerError)
}

// EndpointIDFromRequest returns the endpoint ID from the HTTP request, or an
// empty string if no endpoint ID is specified.
//
// This will check both the 'x-piko-endpoint' header and 'Host' header, where
// x-piko-endpoint takes precedence.
func EndpointIDFromRequest(r *http.Request) string {
endpointID := r.Header.Get("x-piko-endpoint")
if endpointID != "" {
return endpointID
}

// Strip the port if given.
host, _, err := net.SplitHostPort(r.Host)
if err != nil {
host = r.Host
}

if host == "" {
return ""
}
if net.ParseIP(host) != nil {
// Ignore IP addresses.
return ""
}
if strings.Contains(host, ".") {
// If a host is given and contains a separator, use the bottom-level
// domain as the endpoint ID.
//
// Such as if the domain is 'xyz.piko.example.com', then 'xyz' is the
// endpoint ID.
return strings.Split(host, ".")[0]
}

return ""
}

func init() {
// Disable Gin debug logs.
gin.SetMode(gin.ReleaseMode)
Expand Down
Loading

0 comments on commit ed6b423

Please sign in to comment.