-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from go-andiamo/additions
Path vars
- Loading branch information
Showing
14 changed files
with
1,023 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
package urit | ||
|
||
import ( | ||
"errors" | ||
) | ||
|
||
type HeadersOption interface { | ||
GetHeaders() (map[string]string, error) | ||
} | ||
|
||
type Headers interface { | ||
HeadersOption | ||
Set(key string, value interface{}) Headers | ||
Get(key string) (interface{}, bool) | ||
Has(key string) bool | ||
Del(key string) Headers | ||
Clone() Headers | ||
} | ||
|
||
func NewHeaders(namesAndValues ...interface{}) (Headers, error) { | ||
if len(namesAndValues)%2 != 0 { | ||
return nil, errors.New("must be a value for each name") | ||
} | ||
result := &headers{ | ||
entries: map[string]interface{}{}, | ||
} | ||
for i := 0; i < len(namesAndValues)-1; i += 2 { | ||
if k, ok := namesAndValues[i].(string); ok { | ||
result.entries[k] = namesAndValues[i+1] | ||
} else { | ||
return nil, errors.New("name must be a string") | ||
} | ||
} | ||
return result, nil | ||
} | ||
|
||
type headers struct { | ||
entries map[string]interface{} | ||
} | ||
|
||
func (h *headers) GetHeaders() (map[string]string, error) { | ||
result := map[string]string{} | ||
for k, v := range h.entries { | ||
if str, err := getValue(v); err == nil { | ||
result[k] = str | ||
} else { | ||
return result, err | ||
} | ||
} | ||
return result, nil | ||
} | ||
|
||
func (h *headers) Set(key string, value interface{}) Headers { | ||
h.entries[key] = value | ||
return h | ||
} | ||
|
||
func (h *headers) Get(key string) (interface{}, bool) { | ||
v, ok := h.entries[key] | ||
return v, ok | ||
} | ||
|
||
func (h *headers) Has(key string) bool { | ||
_, ok := h.entries[key] | ||
return ok | ||
} | ||
|
||
func (h *headers) Del(key string) Headers { | ||
delete(h.entries, key) | ||
return h | ||
} | ||
|
||
func (h *headers) Clone() Headers { | ||
result := &headers{ | ||
entries: map[string]interface{}{}, | ||
} | ||
for k, v := range h.entries { | ||
result.entries[k] = v | ||
} | ||
return result | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
package urit | ||
|
||
import ( | ||
"github.com/stretchr/testify/require" | ||
"testing" | ||
) | ||
|
||
func TestNewHeaders(t *testing.T) { | ||
h, err := NewHeaders() | ||
require.NoError(t, err) | ||
require.NotNil(t, h) | ||
rh, ok := h.(*headers) | ||
require.True(t, ok) | ||
require.Equal(t, 0, len(rh.entries)) | ||
|
||
h, err = NewHeaders("foo", 1.23) | ||
require.NoError(t, err) | ||
require.NotNil(t, h) | ||
rh, ok = h.(*headers) | ||
require.True(t, ok) | ||
require.Equal(t, 1, len(rh.entries)) | ||
require.Equal(t, 1.23, rh.entries["foo"]) | ||
} | ||
|
||
func TestNewHeadersErrors(t *testing.T) { | ||
_, err := NewHeaders("foo") // must be an even number! | ||
require.Error(t, err) | ||
require.Equal(t, `must be a value for each name`, err.Error()) | ||
|
||
_, err = NewHeaders(true, false) // first must be a string! | ||
require.Error(t, err) | ||
require.Equal(t, `name must be a string`, err.Error()) | ||
} | ||
|
||
func TestHeaders_GetHeaders(t *testing.T) { | ||
h, err := NewHeaders() | ||
require.NoError(t, err) | ||
hds, err := h.GetHeaders() | ||
require.NoError(t, err) | ||
require.Equal(t, 0, len(hds)) | ||
|
||
h, err = NewHeaders("foo", 1.23) | ||
require.NoError(t, err) | ||
hds, err = h.GetHeaders() | ||
require.NoError(t, err) | ||
require.Equal(t, 1, len(hds)) | ||
require.Equal(t, "1.23", hds["foo"]) | ||
|
||
h, err = NewHeaders("foo", nil) | ||
require.NoError(t, err) | ||
_, err = h.GetHeaders() | ||
require.Error(t, err) | ||
require.Equal(t, `unknown value type`, err.Error()) | ||
|
||
h, err = NewHeaders("foo", func() { | ||
// this does not yield a string | ||
}) | ||
require.NoError(t, err) | ||
_, err = h.GetHeaders() | ||
require.Error(t, err) | ||
require.Equal(t, `unknown value type`, err.Error()) | ||
} | ||
|
||
func TestHeaders_GetSet(t *testing.T) { | ||
h, err := NewHeaders("foo", 1) | ||
require.NoError(t, err) | ||
v, ok := h.Get("foo") | ||
require.True(t, ok) | ||
require.Equal(t, 1, v) | ||
h.Set("foo", 2) | ||
v, ok = h.Get("foo") | ||
require.True(t, ok) | ||
require.Equal(t, 2, v) | ||
_, ok = h.Get("bar") | ||
require.False(t, ok) | ||
} | ||
|
||
func TestHeaders_HasDel(t *testing.T) { | ||
h, err := NewHeaders("foo", 1) | ||
require.NoError(t, err) | ||
require.True(t, h.Has("foo")) | ||
h.Del("foo") | ||
require.False(t, h.Has("foo")) | ||
require.False(t, h.Has("bar")) | ||
} | ||
|
||
func TestHeaders_Clone(t *testing.T) { | ||
h1, err := NewHeaders("foo", 1) | ||
require.NoError(t, err) | ||
require.True(t, h1.Has("foo")) | ||
require.False(t, h1.Has("bar")) | ||
h2 := h1.Clone() | ||
require.True(t, h2.Has("foo")) | ||
require.False(t, h2.Has("bar")) | ||
h2.Del("foo") | ||
require.True(t, h1.Has("foo")) | ||
require.False(t, h1.Has("bar")) | ||
require.False(t, h2.Has("foo")) | ||
require.False(t, h2.Has("bar")) | ||
h2.Set("bar", 2) | ||
require.True(t, h1.Has("foo")) | ||
require.False(t, h1.Has("bar")) | ||
require.False(t, h2.Has("foo")) | ||
require.True(t, h2.Has("bar")) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package urit | ||
|
||
type HostOption interface { | ||
GetAddress() string | ||
} | ||
|
||
type Host interface { | ||
HostOption | ||
} | ||
|
||
func NewHost(address string) Host { | ||
return &host{ | ||
address: address, | ||
} | ||
} | ||
|
||
type host struct { | ||
address string | ||
} | ||
|
||
func (h *host) GetAddress() string { | ||
return h.address | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package urit | ||
|
||
import ( | ||
"github.com/stretchr/testify/require" | ||
"testing" | ||
) | ||
|
||
func TestNewHost(t *testing.T) { | ||
h := NewHost(`www.example.com`) | ||
require.NotNil(t, h) | ||
rh, ok := h.(*host) | ||
require.True(t, ok) | ||
require.Equal(t, `www.example.com`, rh.address) | ||
require.Equal(t, `www.example.com`, h.GetAddress()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.