Skip to content

Commit

Permalink
fix(metadata): trim responses before parsing
Browse files Browse the repository at this point in the history
Fixes #540
  • Loading branch information
jooola committed Nov 5, 2024
1 parent 9903f4d commit 2d372ed
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 21 deletions.
7 changes: 6 additions & 1 deletion hcloud/metadata/client.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package metadata

import (
"bytes"
"fmt"
"io"
"net"
Expand Down Expand Up @@ -80,16 +81,20 @@ func NewClient(options ...ClientOption) *Client {
// get executes an HTTP request against the API.
func (c *Client) get(path string) (string, error) {
url := c.endpoint + path

resp, err := c.httpClient.Get(url)
if err != nil {
return "", err
}

defer resp.Body.Close()
bodyBytes, err := io.ReadAll(resp.Body)
if err != nil {
return "", err
}
body := string(bodyBytes)

body := string(bytes.TrimSpace(bodyBytes))

if resp.StatusCode < 200 || resp.StatusCode >= 300 {
return body, fmt.Errorf("response status was %d", resp.StatusCode)
}
Expand Down
65 changes: 45 additions & 20 deletions hcloud/metadata/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

type testEnv struct {
Expand Down Expand Up @@ -39,6 +40,9 @@ func TestClient_Base(t *testing.T) {
env.Mux.HandleFunc("/ok", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("ok"))
})
env.Mux.HandleFunc("/sanitized", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("sanitized \n"))
})
env.Mux.HandleFunc("/not-found", func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(404)
w.Write([]byte("not found"))
Expand All @@ -47,6 +51,9 @@ func TestClient_Base(t *testing.T) {
if body, err := env.Client.get("/ok"); assert.NoError(t, err) {
assert.Equal(t, "ok", body)
}
if body, err := env.Client.get("/sanitized"); assert.NoError(t, err) {
assert.Equal(t, "sanitized", body)
}
if body, err := env.Client.get("/not-found"); assert.EqualError(t, err, "response status was 404") {
assert.Equal(t, "not found", body)
}
Expand Down Expand Up @@ -99,33 +106,51 @@ func TestClient_Hostname(t *testing.T) {
}

func TestClient_InstanceID(t *testing.T) {
env := newTestEnv()
env.Mux.HandleFunc("/instance-id", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("123456"))
t.Run("success", func(t *testing.T) {
env := newTestEnv()
env.Mux.HandleFunc("/instance-id", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("123456"))
})

instanceID, err := env.Client.InstanceID()
require.NoError(t, err)
require.Equal(t, int64(123456), instanceID)
})

instanceID, err := env.Client.InstanceID()
if err != nil {
t.Fatal(err)
}
if instanceID != 123456 {
t.Fatalf("Unexpected instanceID %d", instanceID)
}
t.Run("success sanitized", func(t *testing.T) {
env := newTestEnv()
env.Mux.HandleFunc("/instance-id", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("123456\n"))
})

instanceID, err := env.Client.InstanceID()
require.NoError(t, err)
require.Equal(t, int64(123456), instanceID)
})
}

func TestClient_PublicIPv4(t *testing.T) {
env := newTestEnv()
env.Mux.HandleFunc("/public-ipv4", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("127.0.0.1"))
t.Run("success", func(t *testing.T) {
env := newTestEnv()
env.Mux.HandleFunc("/public-ipv4", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("127.0.0.1"))
})

publicIPv4, err := env.Client.PublicIPv4()
require.NoError(t, err)
require.Equal(t, "127.0.0.1", publicIPv4.String())
})

publicIPv4, err := env.Client.PublicIPv4()
if err != nil {
t.Fatal(err)
}
if publicIPv4.String() != "127.0.0.1" {
t.Fatalf("Unexpected PublicIPv4 %s", publicIPv4.String())
}
t.Run("success sanitized", func(t *testing.T) {
env := newTestEnv()
env.Mux.HandleFunc("/public-ipv4", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("127.0.0.1\n"))
})

publicIPv4, err := env.Client.PublicIPv4()
require.NoError(t, err)
require.Equal(t, "127.0.0.1", publicIPv4.String())
})
}

func TestClient_Region(t *testing.T) {
Expand Down

0 comments on commit 2d372ed

Please sign in to comment.