mirror of
https://github.com/openfaas/faas.git
synced 2025-06-10 01:06:47 +00:00
Changes from pull request feedback
1. Use httptest.ResponseRecorder instead of custom implementation StringResponseWriter. 2. Remove verbose log line in infohandler Signed-off-by: Edward Wilde <ewilde@gmail.com>
This commit is contained in:
parent
aca2c7fe2a
commit
28c9ccd0aa
@ -5,6 +5,9 @@ import (
|
|||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
|
"io/ioutil"
|
||||||
|
"net/http/httptest"
|
||||||
|
|
||||||
"github.com/openfaas/faas/gateway/types"
|
"github.com/openfaas/faas/gateway/types"
|
||||||
"github.com/openfaas/faas/gateway/version"
|
"github.com/openfaas/faas/gateway/version"
|
||||||
)
|
)
|
||||||
@ -12,16 +15,19 @@ import (
|
|||||||
// MakeInfoHandler is responsible for display component version information
|
// MakeInfoHandler is responsible for display component version information
|
||||||
func MakeInfoHandler(h http.Handler) http.HandlerFunc {
|
func MakeInfoHandler(h http.Handler) http.HandlerFunc {
|
||||||
return func(w http.ResponseWriter, r *http.Request) {
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
sw := types.NewStringResponseWriter()
|
responseRecorder := httptest.NewRecorder()
|
||||||
h.ServeHTTP(sw, r)
|
h.ServeHTTP(responseRecorder, r)
|
||||||
|
upstreamCall := responseRecorder.Result()
|
||||||
|
|
||||||
|
defer upstreamCall.Body.Close()
|
||||||
|
|
||||||
log.Printf("Body: %s", sw.Body())
|
|
||||||
provider := make(map[string]interface{})
|
provider := make(map[string]interface{})
|
||||||
providerVersion := &types.VersionInfo{}
|
providerVersion := &types.VersionInfo{}
|
||||||
|
|
||||||
err := json.Unmarshal(sw.Body(), &provider)
|
upstreamBody, _ := ioutil.ReadAll(upstreamCall.Body)
|
||||||
|
err := json.Unmarshal(upstreamBody, &provider)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("Error unmarshalling provider json. Got %s. Error %s\n", string(sw.Body()), err.Error())
|
log.Printf("Error unmarshalling provider json from body %s. Error %s\n", upstreamBody, err.Error())
|
||||||
}
|
}
|
||||||
|
|
||||||
versionMap := provider["version"].(map[string]interface{})
|
versionMap := provider["version"].(map[string]interface{})
|
||||||
|
@ -1,10 +1,5 @@
|
|||||||
package types
|
package types
|
||||||
|
|
||||||
import (
|
|
||||||
"bytes"
|
|
||||||
"net/http"
|
|
||||||
)
|
|
||||||
|
|
||||||
// GatewayInfo provides information about the gateway and it's connected components
|
// GatewayInfo provides information about the gateway and it's connected components
|
||||||
type GatewayInfo struct {
|
type GatewayInfo struct {
|
||||||
Provider *ProviderInfo `json:"provider"`
|
Provider *ProviderInfo `json:"provider"`
|
||||||
@ -24,35 +19,3 @@ type VersionInfo struct {
|
|||||||
SHA string `json:"sha"`
|
SHA string `json:"sha"`
|
||||||
Release string `json:"release"`
|
Release string `json:"release"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// StringResponseWriter captures the handlers HTTP response in a buffer
|
|
||||||
type StringResponseWriter struct {
|
|
||||||
body *bytes.Buffer
|
|
||||||
headerCode int
|
|
||||||
header http.Header
|
|
||||||
}
|
|
||||||
|
|
||||||
// NewStringResponseWriter create a new StringResponseWriter
|
|
||||||
func NewStringResponseWriter() *StringResponseWriter {
|
|
||||||
return &StringResponseWriter{body: &bytes.Buffer{}, header: make(http.Header)}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Header capture the Header information
|
|
||||||
func (s StringResponseWriter) Header() http.Header {
|
|
||||||
return s.header
|
|
||||||
}
|
|
||||||
|
|
||||||
// Write captures the response data
|
|
||||||
func (s StringResponseWriter) Write(data []byte) (int, error) {
|
|
||||||
return s.body.Write(data)
|
|
||||||
}
|
|
||||||
|
|
||||||
// WriteHeader captures the status code of the response
|
|
||||||
func (s StringResponseWriter) WriteHeader(statusCode int) {
|
|
||||||
s.headerCode = statusCode
|
|
||||||
}
|
|
||||||
|
|
||||||
// Body returns the response body bytes
|
|
||||||
func (s StringResponseWriter) Body() []byte {
|
|
||||||
return s.body.Bytes()
|
|
||||||
}
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user