Local Tests for HttpJsonPoster

This commit is contained in:
DanB
2015-10-01 20:23:59 +02:00
parent 75dd37f90d
commit f75afcea33
2 changed files with 52 additions and 1 deletions

View File

@@ -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

View File

@@ -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 <http://www.gnu.org/licenses/>
*/
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))
}
}