Skip to content

Commit

Permalink
test: update config options tests
Browse files Browse the repository at this point in the history
  • Loading branch information
waltjones committed Sep 25, 2019
1 parent bf51127 commit a7aa47e
Showing 1 changed file with 90 additions and 10 deletions.
100 changes: 90 additions & 10 deletions client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -296,16 +296,19 @@ func TestLambdaWrapperWithMessage(t *testing.T) {

func TestGettersAndSetters_Default(t *testing.T) {
c := testClient()
c.Transport = &TestTransport{}
testGettersAndSetters(c, t)
}

func TestGettersAndSetters_Async(t *testing.T) {
c := rollbar.NewAsync("", "", "", "", "")
c.Transport = &TestTransport{}
testGettersAndSetters(c, t)
}

func TestGettersAndSetters_Sync(t *testing.T) {
c := rollbar.NewSync("", "", "", "", "")
c.Transport = &TestTransport{}
testGettersAndSetters(c, t)
}

Expand All @@ -317,8 +320,10 @@ func testGettersAndSetters(client *rollbar.Client, t *testing.T) {
codeVersion := "CodeVersion"
host := "SomeHost"
root := "////"
fingerprint := true
scrubHeaders := regexp.MustCompile("Foo")
scrubFields := regexp.MustCompile("squirrel|doggo")
captureIP := rollbar.CaptureIpNone

errorIfEqual(token, client.Token(), t)
errorIfEqual(environment, client.Environment(), t)
Expand All @@ -327,6 +332,10 @@ func testGettersAndSetters(client *rollbar.Client, t *testing.T) {
errorIfEqual(codeVersion, client.CodeVersion(), t)
errorIfEqual(host, client.ServerHost(), t)
errorIfEqual(root, client.ServerRoot(), t)
errorIfEqual(fingerprint, client.Fingerprint(), t)
errorIfEqual(captureIP, client.CaptureIp(), t)
errorIfEqual(scrubHeaders, client.ScrubHeaders(), t)
errorIfEqual(scrubFields, client.ScrubFields(), t)

if client.Fingerprint() {
t.Error("expected fingerprint to default to false")
Expand All @@ -349,10 +358,11 @@ func testGettersAndSetters(client *rollbar.Client, t *testing.T) {
client.SetCodeVersion(codeVersion)
client.SetServerHost(host)
client.SetServerRoot(root)
client.SetFingerprint(true)
client.SetFingerprint(fingerprint)
client.SetLogger(&rollbar.SilentClientLogger{})
client.SetScrubHeaders(scrubHeaders)
client.SetScrubFields(scrubFields)
client.SetCaptureIp(captureIP)

client.SetEnabled(true)

Expand All @@ -363,6 +373,10 @@ func testGettersAndSetters(client *rollbar.Client, t *testing.T) {
errorIfNotEqual(codeVersion, client.CodeVersion(), t)
errorIfNotEqual(host, client.ServerHost(), t)
errorIfNotEqual(root, client.ServerRoot(), t)
errorIfNotEqual(fingerprint, client.Fingerprint(), t)
errorIfNotEqual(captureIP, client.CaptureIp(), t)
errorIfNotEqual(scrubHeaders, client.ScrubHeaders(), t)
errorIfNotEqual(scrubFields, client.ScrubFields(), t)

if !client.Fingerprint() {
t.Error("expected fingerprint to default to false")
Expand All @@ -375,15 +389,39 @@ func testGettersAndSetters(client *rollbar.Client, t *testing.T) {
if !client.ScrubFields().MatchString("squirrel") {
t.Error("expected matching scrub field")
}

client.ErrorWithLevel(rollbar.ERR, errors.New("Bork"))

if transport, ok := client.Transport.(*TestTransport); ok {
body := transport.Body
if body["data"] == nil {
t.Error("body should have data")
}
data := body["data"].(map[string]interface{})
configuredOptions := configuredOptionsFromData(data)

errorIfNotEqual(environment, configuredOptions["environment"].(string), t)
errorIfNotEqual(endpoint, configuredOptions["endpoint"].(string), t)
errorIfNotEqual(platform, configuredOptions["platform"].(string), t)
errorIfNotEqual(codeVersion, configuredOptions["codeVersion"].(string), t)
errorIfNotEqual(host, configuredOptions["serverHost"].(string), t)
errorIfNotEqual(root, configuredOptions["serverRoot"].(string), t)
errorIfNotEqual(fingerprint, configuredOptions["fingerprint"].(bool), t)
errorIfNotEqual(scrubHeaders, configuredOptions["scrubHeaders"].(*regexp.Regexp), t)
errorIfNotEqual(scrubFields, configuredOptions["scrubFields"].(*regexp.Regexp), t)

} else {
t.Fail()
}
}

func errorIfEqual(a, b string, t *testing.T) {
func errorIfEqual(a, b interface{}, t *testing.T) {
if a == b {
t.Error("Expected", a, " != ", b)
}
}

func errorIfNotEqual(a, b string, t *testing.T) {
func errorIfNotEqual(a, b interface{}, t *testing.T) {
if a != b {
t.Error("Expected", a, " == ", b)
}
Expand All @@ -409,6 +447,13 @@ func TestSetPerson(t *testing.T) {
errorIfNotEqual(id, person["id"], t)
errorIfNotEqual(username, person["username"], t)
errorIfNotEqual(email, person["email"], t)

configuredOptions := configuredOptionsFromData(data)
configuredPerson := configuredOptions["person"].(map[string]string)

errorIfNotEqual(id, configuredPerson["Id"], t)
errorIfNotEqual(username, configuredPerson["Username"], t)
errorIfNotEqual(email, configuredPerson["Email"], t)
} else {
t.Fail()
}
Expand Down Expand Up @@ -462,6 +507,48 @@ func TestTransform(t *testing.T) {
}
}

func TestSetUnwrapper(t *testing.T) {
client := testClient()
client.SetUnwrapper(rollbar.DefaultUnwrapper)

client.ErrorWithLevel(rollbar.ERR, errors.New("Bork"))

if transport, ok := client.Transport.(*TestTransport); ok {
body := transport.Body
if body["data"] == nil {
t.Error("body should have data")
}
data := body["data"].(map[string]interface{})
configuredOptions := configuredOptionsFromData(data)
if !strings.Contains(configuredOptions["unwrapper"].(string), "func1") {
t.Error("data should have unwrapper in diagnostic object")
}
} else {
t.Fail()
}
}

func TestSetStackTracer(t *testing.T) {
client := testClient()
client.SetStackTracer(rollbar.DefaultStackTracer)

client.ErrorWithLevel(rollbar.ERR, errors.New("Bork"))

if transport, ok := client.Transport.(*TestTransport); ok {
body := transport.Body
if body["data"] == nil {
t.Error("body should have data")
}
data := body["data"].(map[string]interface{})
configuredOptions := configuredOptionsFromData(data)
if !strings.Contains(configuredOptions["stackTracer"].(string), "func2") {
t.Error("data should have stackTracer in diagnostic object")
}
} else {
t.Fail()
}
}

func TestEnabled(t *testing.T) {
client := testClient()
client.SetEnabled(false)
Expand Down Expand Up @@ -496,10 +583,3 @@ func configuredOptionsFromData(data map[string]interface{}) map[string]interface
configuredOptions := diagnostic["configuredOptions"].(map[string]interface{})
return configuredOptions
}

func framesLenFromData(data map[string]interface{}) int {
body := data["body"].(map[string]interface{})
traceChain := body["trace_chain"].([]map[string]interface{})
frames := reflect.ValueOf(traceChain[0]["frames"])
return frames.Len()
}

0 comments on commit a7aa47e

Please sign in to comment.