Skip to content
Permalink
Browse files

(parser): Add headers support to the record parser

  • Loading branch information
erbesharat committed Jan 4, 2020
1 parent f4318a5 commit 7e05f2a971d5e2a37a5dd28ba7e6d67e8b2e421b
Showing with 43 additions and 2 deletions.
  1. +9 −2 record.go
  2. +34 −0 record_test.go
@@ -34,6 +34,7 @@ type record struct {
Root string
Re string
Ref bool
Headers map[string]string
}

// getRecord uses the given host to find a TXT record
@@ -71,12 +72,15 @@ func getRecord(host string, c Config, w http.ResponseWriter, r *http.Request) (r
return rec, nil
}

// Parse takes a string containing the DNS TXT record and returns
// ParseRecord takes a string containing the DNS TXT record and returns
// a TXTDirect record struct instance.
// It will return an error if the DNS TXT record is not standard or
// if the record type is not enabled in the TXTDirect's config.
func ParseRecord(str string, w http.ResponseWriter, req *http.Request, c Config) (record, error) {
r := record{}
r := record{
Headers: map[string]string{},
}

s := strings.Split(str, ";")
for _, l := range s {
switch {
@@ -142,6 +146,9 @@ func ParseRecord(str string, w http.ResponseWriter, req *http.Request, c Config)
l = strings.TrimPrefix(l, "website=")
l = ParseURI(l, w, req, c)
r.Website = l
case strings.HasPrefix(l, ">"):
header := strings.Split(l, "=")
r.Headers[header[0][1:]] = header[1]

default:
tuple := strings.Split(l, "=")
@@ -156,6 +156,29 @@ func TestParseRecord(t *testing.T) {
},
status: 404,
},
{
txtRecord: "v=txtv0;type=path;code=302;>Header-1=HeaderValue",
expected: record{
Version: "txtv0",
Type: "path",
Code: 302,
Ref: false,
Headers: map[string]string{"Header-1": "HeaderValue"},
},
},
{
txtRecord: "v=txtv0;type=path;code=302;>Header-1=HeaderValue;>Header-2=HeaderValue",
expected: record{
Version: "txtv0",
Type: "path",
Code: 302,
Ref: false,
Headers: map[string]string{
"Header-1": "HeaderValue",
"Header-2": "HeaderValue",
},
},
},
}

for i, test := range tests {
@@ -200,5 +223,16 @@ func TestParseRecord(t *testing.T) {
if got, want := r.Vcs, test.expected.Vcs; got != want {
t.Errorf("Test %d: Expected Vcs to be '%s', got '%s'", i, want, got)
}

if len(r.Headers) != len(test.expected.Headers) {
t.Errorf("Test %d: Expected %d headers, got '%d'", i, len(r.Headers), len(test.expected.Headers))
}

for header, val := range r.Headers {
if test.expected.Headers[header] != val {
t.Errorf("Test %d: Expected %s Header to be '%s', got '%s'",
i, header, test.expected.Headers[header], val)
}
}
}
}

0 comments on commit 7e05f2a

Please sign in to comment.
You can’t perform that action at this time.