Skip to content

Commit

Permalink
Add more tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
Denis Krivak committed Sep 4, 2023
1 parent 308d4b0 commit 80a2d95
Show file tree
Hide file tree
Showing 4 changed files with 142 additions and 1 deletion.
89 changes: 89 additions & 0 deletions connectbox_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package main

import (
"context"
"io"
"net/http"
"net/http/httptest"
"testing"

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

func TestNewConnectBox(t *testing.T) {
t.Run("valid config", func(t *testing.T) {
client, err := NewConnectBox("127.0.0.1:8080", "bob", "qwerty")
require.NoError(t, err)
require.Equal(t, "http://127.0.0.1:8080", client.addr)
require.Equal(t, "bob", client.username)
require.Equal(t,
"65e84be33532fb784c48129675f9eff3a682b27168c0ea744b2cf58ee02337c5",
client.password)
})

t.Run("invalid address", func(t *testing.T) {
_, err := NewConnectBox("hello, world!", "bob", "qwerty")
require.ErrorContains(t, err, "invalid address")
})
}

func TestConnectBox_xmlRequest(t *testing.T) {
t.Run("valid response", func(t *testing.T) {
ctx := context.Background()

connectbox := testConnectBox{
status: http.StatusOK,
resp: "hello, world",
}
server := httptest.NewServer(&connectbox)
defer server.Close()

client, err := NewConnectBox(server.URL, "bob", "qwerty")
require.NoError(t, err)
client.token = "abc"

args := xmlArgs{{"key", "value"}}
resp, err := client.xmlRequest(ctx, "/test", "100", args)
require.NoError(t, err)

want := "token=abc&fun=100&key=value"
require.Equal(t, want, connectbox.req)
want = "hello, world"
require.Equal(t, want, resp)
})

t.Run("invalid status code", func(t *testing.T) {
ctx := context.Background()

connectbox := testConnectBox{
status: http.StatusInternalServerError,
}
server := httptest.NewServer(&connectbox)
defer server.Close()

client, err := NewConnectBox(server.URL, "bob", "qwerty")
require.NoError(t, err)
client.token = "abc"

args := xmlArgs{{"key", "value"}}
_, err = client.xmlRequest(ctx, "/test", "100", args)
require.ErrorContains(t, err, "invalid response status")
})
}

type testConnectBox struct {
path string
req string
status int
resp string
}

func (t *testConnectBox) ServeHTTP(w http.ResponseWriter, r *http.Request) {
t.path = r.URL.Path
body, err := io.ReadAll(r.Body)
if err == nil {
t.req = string(body)
}
w.WriteHeader(t.status)
w.Write([]byte(t.resp))
}
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,18 @@ go 1.21

require (
github.com/prometheus/client_golang v1.16.0
github.com/stretchr/testify v1.8.4
gopkg.in/yaml.v3 v3.0.1
)

require (
github.com/beorn7/perks v1.0.1 // indirect
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/kr/text v0.2.0 // indirect
github.com/matttproud/golang_protobuf_extensions v1.0.4 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/prometheus/client_model v0.4.0 // indirect
github.com/prometheus/common v0.44.0 // indirect
github.com/prometheus/procfs v0.11.1 // indirect
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
github.com/matttproud/golang_protobuf_extensions v1.0.4 h1:mmDVorXM7PCGKw94cs5zkfA9PSy5pEvNWRP0ET0TIVo=
github.com/matttproud/golang_protobuf_extensions v1.0.4/go.mod h1:BSXmuO+STAnVfrANrmjBb36TMTDstsz7MSK+HVaYKv4=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/prometheus/client_golang v1.16.0 h1:yk/hx9hDbrGHovbci4BY+pRMfSuuat626eFsHb7tmT8=
github.com/prometheus/client_golang v1.16.0/go.mod h1:Zsulrv/L9oM40tJ7T815tM89lFEugiJ9HzIqaAx4LKc=
github.com/prometheus/client_model v0.4.0 h1:5lQXD3cAg1OXBf4Wq03gTrXHeaV0TQvGfUooCfx1yqY=
Expand All @@ -28,6 +30,8 @@ github.com/prometheus/procfs v0.11.1 h1:xRC8Iq1yyca5ypa9n1EZnWZkt7dwcoRPQwX/5gwa
github.com/prometheus/procfs v0.11.1/go.mod h1:eesXgaPo1q7lBpVMoMy0ZOFTth9hBn4W/y0/p/ScXhY=
github.com/rogpeppe/go-internal v1.11.0 h1:cWPaGQEPrBb5/AsnsZesgZZ9yb1OQ+GOISoDNXVBh4M=
github.com/rogpeppe/go-internal v1.11.0/go.mod h1:ddIwULY96R17DhadqLgMfk9H9tvdUzkipdSkR5nkCZA=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.12.0 h1:CM0HF96J0hcLAwsHPJZjfdNzs0gftsLfgKt57wWHJ0o=
golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
Expand Down
47 changes: 46 additions & 1 deletion metrics_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,51 @@
package main

import "testing"
import (
"encoding/xml"
"testing"

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

func TestCMState_UnmarshalXML(t *testing.T) {
t.Run("valid xml", func(t *testing.T) {
data := `<?xml version="1.0" encoding="utf-8"?>` +
`<cmstate>` +
`<TunnerTemperature>80</TunnerTemperature>` +
`<Temperature>60</Temperature>` +
`<OperState>OPERATIONAL</OperState>` +
`<wan_ipv4_addr>1.1.1.1</wan_ipv4_addr>` +
`<wan_ipv6_addr>` +
`<wan_ipv6_addr_entry>0000:0000:0000:0000:0000:0000:0000:0001/128</wan_ipv6_addr_entry>` +
`<wan_ipv6_addr_entry>0000:0000:0000:0000:0000:0000:0000:0002/128</wan_ipv6_addr_entry>` +
`</wan_ipv6_addr>` +
`</cmstate>`

var cmstate CMState
err := xml.Unmarshal([]byte(data), &cmstate)
require.NoError(t, err)

expected := CMState{
TunnerTemperature: 26,
Temperature: 15,
OperState: "OPERATIONAL",
WANIPv4Addr: "1.1.1.1",
WANIPv6Addrs: []string{
"0000:0000:0000:0000:0000:0000:0000:0001/128",
"0000:0000:0000:0000:0000:0000:0000:0002/128",
},
}
require.Equal(t, expected, cmstate)
})

t.Run("invalid xml", func(t *testing.T) {
data := `<?xml version="1.0" encoding="utf-8"?><cmstate>`

var cmstate CMState
err := xml.Unmarshal([]byte(data), &cmstate)
require.ErrorContains(t, err, "XML syntax error")
})
}

func TestFahrenheitToCelsius(t *testing.T) {
testCases := []struct {
Expand Down

0 comments on commit 80a2d95

Please sign in to comment.