Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DNS update with FQDN in domain field, main.go #64

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Update request_handler_test.go
gmkey authored Feb 16, 2021

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
commit 53e6ad0142a419ed4bde116abaa08ea9514659d1
64 changes: 62 additions & 2 deletions rest-api/request_handler_test.go
Original file line number Diff line number Diff line change
@@ -3,12 +3,18 @@ package main
import (
"net/http"
"testing"
"strings"
)

var defaultExtractor = RequestDataExtractor{
Address: func(r *http.Request) string { return r.URL.Query().Get("addr") },
Secret: func(r *http.Request) string { return r.URL.Query().Get("secret") },
Domain: func(r *http.Request) string { return r.URL.Query().Get("domain") },
Domain: func(r *http.Request) string {
confDomain := "." + appConfig.Domain
srcDomain := r.URL.Query().Get("domain")
srcDomain = strings.Replace(srcDomain, confDomain, "", -1)
return srcDomain
},
}

var dynExtractor = RequestDataExtractor{
@@ -21,7 +27,12 @@ var dynExtractor = RequestDataExtractor{

return sharedSecret
},
Domain: func(r *http.Request) string { return r.URL.Query().Get("hostname") },
Domain: func(r *http.Request) string {
confDomain := "." + appConfig.Domain
srcDomain := r.URL.Query().Get("hostname")
srcDomain = strings.Replace(srcDomain, confDomain, "", -1)
return srcDomain
},
}

func TestBuildWebserviceResponseFromRequestToReturnValidObject(t *testing.T) {
@@ -48,6 +59,31 @@ func TestBuildWebserviceResponseFromRequestToReturnValidObject(t *testing.T) {
}
}

func TestBuildWebserviceResponseFromRequestToReturnValidObjectLongDom(t *testing.T) {
var appConfig = &Config{}
appConfig.SharedSecret = "changeme"

req, _ := http.NewRequest("GET", "/update?secret=changeme&domain=foo.example.org&addr=1.2.3.4", nil)
result := BuildWebserviceResponseFromRequest(req, appConfig, defaultExtractor)

if result.Success != true {
t.Fatalf("Expected WebserviceResponse.Success to be true")
}

if result.Domain != "foo" {
t.Fatalf("Expected WebserviceResponse.Domain to be foo")
}

if result.Address != "1.2.3.4" {
t.Fatalf("Expected WebserviceResponse.Address to be 1.2.3.4")
}

if result.AddrType != "A" {
t.Fatalf("Expected WebserviceResponse.AddrType to be A")
}
}


func TestBuildWebserviceResponseFromRequestWithXRealIPHeaderToReturnValidObject(t *testing.T) {
var appConfig = &Config{}
appConfig.SharedSecret = "changeme"
@@ -73,6 +109,30 @@ func TestBuildWebserviceResponseFromRequestWithXRealIPHeaderToReturnValidObject(
}
}

func TestBuildWebserviceResponseFromRequestWithXRealIPHeaderToReturnValidObjectLongDom(t *testing.T) {
var appConfig = &Config{}
appConfig.SharedSecret = "changeme"

req, _ := http.NewRequest("GET", "/update?secret=changeme&domain=foo.example.org", nil)
req.Header.Add("X-Real-Ip", "1.2.3.4")
result := BuildWebserviceResponseFromRequest(req, appConfig, defaultExtractor)

if result.Success != true {
t.Fatalf("Expected WebserviceResponse.Success to be true")
}

if result.Domain != "foo" {
t.Fatalf("Expected WebserviceResponse.Domain to be foo")
}

if result.Address != "1.2.3.4" {
t.Fatalf("Expected WebserviceResponse.Address to be 1.2.3.4")
}

if result.AddrType != "A" {
t.Fatalf("Expected WebserviceResponse.AddrType to be A")
}
}
func TestBuildWebserviceResponseFromRequestWithXForwardedForHeaderToReturnValidObject(t *testing.T) {
var appConfig = &Config{}
appConfig.SharedSecret = "changeme"