httpUrlDP.FieldAsInterface method with tests

This commit is contained in:
DanB
2018-06-27 12:26:09 +02:00
parent ce91d21468
commit ecb2b1753d
2 changed files with 75 additions and 8 deletions

View File

@@ -54,7 +54,7 @@ func newHAReplyEncoder(encType string,
}
func newHTTPUrlDP(req *http.Request) (dP engine.DataProvider, err error) {
dP = &httpUrlDP{req: req}
dP = &httpUrlDP{req: req, cache: engine.NewNavigableMap(nil)}
return
}
@@ -62,25 +62,43 @@ func newHTTPUrlDP(req *http.Request) (dP engine.DataProvider, err error) {
// decoded data is only searched once and cached
type httpUrlDP struct {
req *http.Request
cache engine.NavigableMap
cache *engine.NavigableMap
}
// String is part of engine.DataProvider interface
func (url *httpUrlDP) String() string {
return utils.ToJSON(url.cache.AsMapStringInterface())
// when called, it will display the already parsed values out of cache
func (hU *httpUrlDP) String() string {
return utils.ToJSON(hU.cache.AsMapStringInterface())
}
// FieldAsInterface is part of engine.DataProvider interface
func (url *httpUrlDP) FieldAsInterface(fldPath []string) (data interface{}, err error) {
func (hU *httpUrlDP) FieldAsInterface(fldPath []string) (data interface{}, err error) {
if len(fldPath) != 1 {
return nil, utils.ErrNotFound
}
if data, err = hU.cache.FieldAsInterface(fldPath); err == nil ||
err != utils.ErrNotFound { // item found in cache
return
}
err = nil // cancel previous err
data = hU.req.FormValue(fldPath[0])
hU.cache.Set(fldPath, data, false)
return
}
// FieldAsString is part of engine.DataProvider interface
func (url *httpUrlDP) FieldAsString(fldPath []string) (data string, err error) {
func (hU *httpUrlDP) FieldAsString(fldPath []string) (data string, err error) {
var valIface interface{}
valIface, err = hU.FieldAsInterface(fldPath)
if err != nil {
return
}
data, _ = utils.CastFieldIfToString(valIface)
return
}
// AsNavigableMap is part of engine.DataProvider interface
func (url *httpUrlDP) AsNavigableMap([]*config.CfgCdrField) (nm *engine.NavigableMap, err error) {
return
func (hU *httpUrlDP) AsNavigableMap([]*config.CfgCdrField) (
nm *engine.NavigableMap, err error) {
return nil, utils.ErrNotImplemented
}

View File

@@ -0,0 +1,49 @@
/*
Real-time Online/Offline Charging System (OCS) for Telecom & ISP environments
Copyright (C) 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 agents
import (
"bufio"
"net/http"
"strings"
"testing"
//"github.com/cgrates/cgrates/utils"
)
func TestHttpUrlDPFieldAsInterface(t *testing.T) {
br := bufio.NewReader(strings.NewReader(`GET /cdr?request_type=MOSMS_CDR&timestamp=2008-08-15%2017:49:21&message_date=2008-08-15%2017:49:21&transactionid=100744&CDR_ID=123456&carrierid=1&mcc=222&mnc=10&imsi=235180000000000&msisdn=%2B4977000000000&destination=%2B497700000001&message_status=0&IOT=0&service_id=1 HTTP/1.1
Host: api.cgrates.org
`))
req, err := http.ReadRequest(br)
if err != nil {
t.Error(err)
}
hU, _ := newHTTPUrlDP(req)
if data, err := hU.FieldAsString([]string{"request_type"}); err != nil {
t.Error(err)
} else if data != "MOSMS_CDR" {
t.Errorf("expecting: MOSMS_CDR, received: <%s>", data)
}
if data, err := hU.FieldAsString([]string{"nonexistent"}); err != nil {
t.Error(err)
} else if data != "" {
t.Errorf("received: <%s>", data)
}
}