Skip to content

Commit

Permalink
Minor style fixes and test tweaks for misc.go and misc_test.go.
Browse files Browse the repository at this point in the history
  • Loading branch information
abbot committed Jan 8, 2016
1 parent e6ed41b commit 8586533
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 43 deletions.
61 changes: 28 additions & 33 deletions misc.go
Expand Up @@ -9,9 +9,7 @@ import (
"strings"
)

/*
Return a random 16-byte base64 alphabet string
*/
// RandomKey returns a random 16-byte base64 alphabet string
func RandomKey() string {
k := make([]byte, 12)
for bytes := 0; bytes < len(k); {
Expand All @@ -24,49 +22,46 @@ func RandomKey() string {
return base64.StdEncoding.EncodeToString(k)
}

/*
H function for MD5 algorithm (returns a lower-case hex MD5 digest)
*/
// H function for MD5 algorithm (returns a lower-case hex MD5 digest)
func H(data string) string {
digest := md5.New()
digest.Write([]byte(data))
return fmt.Sprintf("%x", digest.Sum(nil))
}

/*
ParseList parses a comma-separated list of values as described by RFC 2068.
which was itself ported from urllib2.parse_http_list, from the Python standard library.
Lifted from https://code.google.com/p/gorilla/source/browse/http/parser/parser.go
*/
// ParseList parses a comma-separated list of values as described by
// RFC 2068 and returns list elements.
//
// Lifted from https://code.google.com/p/gorilla/source/browse/http/parser/parser.go
// which was ported from urllib2.parse_http_list, from the Python
// standard library.
func ParseList(value string) []string {
var list []string
var escape, quote bool
b := new(bytes.Buffer)
for _, r := range value {
if escape {
switch {
case escape:
b.WriteRune(r)
escape = false
continue
}
if quote {
case quote:
if r == '\\' {
escape = true
continue
} else if r == '"' {
quote = false
} else {
if r == '"' {
quote = false
}
b.WriteRune(r)
}
b.WriteRune(r)
continue
}
if r == ',' {
case r == ',':
list = append(list, strings.TrimSpace(b.String()))
b.Reset()
continue
}
if r == '"' {
case r == '"':
quote = true
b.WriteRune(r)
default:
b.WriteRune(r)
}
b.WriteRune(r)
}
// Append last part.
if s := b.String(); s != "" {
Expand All @@ -75,13 +70,13 @@ func ParseList(value string) []string {
return list
}

/*
ParsePairs extracts key/value pairs from a comma-separated list of values as
described by RFC 2068.
The resulting values are unquoted. If a value doesn't contain a "=", the
key is the value itself and the value is an empty string.
Lifted from https://code.google.com/p/gorilla/source/browse/http/parser/parser.go
*/
// ParsePairs extracts key/value pairs from a comma-separated list of
// values as described by RFC 2068 and returns a map[key]value. The
// resulting values are unquoted. If a list element doesn't contain a
// "=", the key is the element itself and the value is an empty
// string.
//
// Lifted from https://code.google.com/p/gorilla/source/browse/http/parser/parser.go
func ParsePairs(value string) map[string]string {
m := make(map[string]string)
for _, pair := range ParseList(strings.TrimSpace(value)) {
Expand Down
16 changes: 6 additions & 10 deletions misc_test.go
@@ -1,7 +1,6 @@
package auth

import (
"fmt"
"reflect"
"testing"
)
Expand All @@ -16,11 +15,11 @@ func TestH(t *testing.T) {
}

func TestParsePairs(t *testing.T) {
const header = `username="test", realm="", nonce="FRPnGdb8lvM1UHhi", uri="/css?family=Source+Sans+Pro:400,700,400italic,700italic|Source+Code+Pro", algorithm=MD5, response="fdcdd78e5b306ffed343d0ec3967f2e5", opaque="lEgVjogmIar2fg/t", qop=auth, nc=00000001, cnonce="e76b05db27a3b323"`
const header = `username="\test", realm="a \"quoted\" string", nonce="FRPnGdb8lvM1UHhi", uri="/css?family=Source+Sans+Pro:400,700,400italic,700italic|Source+Code+Pro", algorithm=MD5, response="fdcdd78e5b306ffed343d0ec3967f2e5", opaque="lEgVjogmIar2fg/t", qop=auth, nc=00000001, cnonce="e76b05db27a3b323"`

expected := map[string]string{
want := map[string]string{
"username": "test",
"realm": "",
"realm": `a "quoted" string`,
"nonce": "FRPnGdb8lvM1UHhi",
"uri": "/css?family=Source+Sans+Pro:400,700,400italic,700italic|Source+Code+Pro",
"algorithm": "MD5",
Expand All @@ -30,12 +29,9 @@ func TestParsePairs(t *testing.T) {
"nc": "00000001",
"cnonce": "e76b05db27a3b323",
}
got := ParsePairs(header)

res := ParsePairs(header)

if !reflect.DeepEqual(res, expected) {
fmt.Printf("%#v\n", res)
t.Fatal("Failed to correctly parse pairs")
if !reflect.DeepEqual(got, want) {
t.Fatalf("failed to correctly parse pairs, got %v, want %v\ndiff: %s", got, want)
}

}

0 comments on commit 8586533

Please sign in to comment.