Skip to content
This repository has been archived by the owner on Oct 16, 2023. It is now read-only.

Commit

Permalink
Take an io.Reader and use local test data
Browse files Browse the repository at this point in the history
  • Loading branch information
sampointer committed Dec 5, 2020
1 parent bf1701f commit 6bc5fea
Show file tree
Hide file tree
Showing 3 changed files with 26,438 additions and 13 deletions.
14 changes: 4 additions & 10 deletions ranges/ranges.go
Expand Up @@ -3,9 +3,9 @@ package ranges
import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net"
"net/http"
"strconv"
"time"
)
Expand Down Expand Up @@ -77,21 +77,15 @@ func (r *Ranges) LookupIPv6(ip net.IP) ([]PrefixIPv6, error) {
}

//New is a constructor for Ranges
func New(client *http.Client) (*Ranges, error) {
func New(r io.Reader) (*Ranges, error) {
var ranges Ranges

res, err := client.Get(url)
doc, err := ioutil.ReadAll(r)
if err != nil {
return nil, err
}

defer res.Body.Close()
body, err := ioutil.ReadAll(res.Body)
if err != nil {
return nil, err
}

err = json.Unmarshal(body, &ranges)
err = json.Unmarshal(doc, &ranges)
if err != nil {
return nil, err
}
Expand Down
11 changes: 8 additions & 3 deletions ranges/ranges_test.go
Expand Up @@ -2,26 +2,31 @@ package ranges

import (
"net"
"net/http"
"os"
"testing"

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

func TestRanges(t *testing.T) {
client := new(http.Client)
ranges, err := New(client)
doc, err := os.Open("../test/ip-ranges.json")
require.NoError(t, err)

ranges, err := New(doc)
require.NoError(t, err)

t.Run("has IPv4 prefixes", func(t *testing.T) {
t.Parallel()
require.NotZero(t, ranges.PrefixesIPv4, "should have 1 or more prefixes")
})

t.Run("has IPv6 prefixes", func(t *testing.T) {
t.Parallel()
require.NotZero(t, ranges.PrefixesIPv6, "should have 1 or more prefixes")
})

t.Run("returns a Prefix struct for an IPv4 address", func(t *testing.T) {
t.Parallel()
prefix := PrefixIPv4{
IPPrefix: "52.94.76.0/22",
Region: "us-west-2",
Expand Down

0 comments on commit 6bc5fea

Please sign in to comment.