From f75afcea33b6d70ef6a1884e5e93e717c99b63ff Mon Sep 17 00:00:00 2001 From: DanB Date: Thu, 1 Oct 2015 20:23:59 +0200 Subject: [PATCH] Local Tests for HttpJsonPoster --- utils/{http_client.go => httpclient.go} | 2 +- utils/httpclient_local_test.go | 51 +++++++++++++++++++++++++ 2 files changed, 52 insertions(+), 1 deletion(-) rename utils/{http_client.go => httpclient.go} (95%) create mode 100644 utils/httpclient_local_test.go diff --git a/utils/http_client.go b/utils/httpclient.go similarity index 95% rename from utils/http_client.go rename to utils/httpclient.go index ffed8d037..f9589177d 100644 --- a/utils/http_client.go +++ b/utils/httpclient.go @@ -55,7 +55,7 @@ func HttpJsonPost(url string, skipTlsVerify bool, content interface{}) ([]byte, } // Post with built-in failover -func HttpPoster(url string, skipTlsVerify bool, content interface{}, retries int, fallbackFilePath string) ([]byte, error) { +func HttpJsonPoster(url string, skipTlsVerify bool, content interface{}, retries int, fallbackFilePath string) ([]byte, error) { body, err := json.Marshal(content) if err != nil { return nil, err diff --git a/utils/httpclient_local_test.go b/utils/httpclient_local_test.go new file mode 100644 index 000000000..a8ee14206 --- /dev/null +++ b/utils/httpclient_local_test.go @@ -0,0 +1,51 @@ +/* +Real-time Charging System for Telecom & ISP environments +Copyright (C) 2012-2015 ITsysCOM GmbH + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program. If not, see +*/ + +package utils + +import ( + "encoding/json" + "flag" + "io/ioutil" + "reflect" + "testing" +) + +var testLocal = flag.Bool("local", false, "Perform the tests only on local test environment, not by default.") // This flag will be passed here via "go test -local" args + +type TestContent struct { + Var1 string + Var2 string +} + +func TestHttpJsonPoster(t *testing.T) { + if !*testLocal { + return + } + content := &TestContent{Var1: "Val1", Var2: "Val2"} + filePath := "/tmp/test_http_poster.cgr" + if _, err := HttpJsonPoster("http://localhost:8080/invalid", true, content, 3, filePath); err != nil { + t.Error(err) + } + jsnContent, _ := json.Marshal(content) + if readBytes, err := ioutil.ReadFile(filePath); err != nil { + t.Error(err) + } else if !reflect.DeepEqual(jsnContent, readBytes) { + t.Errorf("Expecting: %q, received: %q", string(jsnContent), string(readBytes)) + } +}