/* 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 */ package agents import ( "bufio" "bytes" "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×tamp=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{"transactionid"}); err != nil { t.Error(err) } else if data != "100744" { 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) } } /* SampleAPIMethod 497924804904 */ func TestHttpXmlDPFieldAsInterface(t *testing.T) { body := ` 2005-08-26T14:16:42 2005-08-26T14:16:56 2005-08-26T14:17:34 My Call Reference 386 sampleusername 1 Conecto LLC US$0.21 yes no US$0.13 44 +441624828505 Isle of Man 38 US$0.0200 US$0.0140 US$0.0130 US$0.0082 +44 7624 494075 Isle of Man 37 US$0.2700 US$0.1890 US$0.1880 US$0.1159 ` req, err := http.NewRequest("POST", "http://localhost:8080/", bytes.NewBuffer([]byte(body))) if err != nil { t.Error(err) } dP, _ := newHTTPXmlDP(req) if data, err := dP.FieldAsString([]string{"complete-success-notification", "userid"}); err != nil { t.Error(err) } else if data != "386" { t.Errorf("expecting: 386, received: <%s>", data) } if data, err := dP.FieldAsString([]string{"complete-success-notification", "username"}); err != nil { t.Error(err) } else if data != "sampleusername" { t.Errorf("expecting: sampleusername, received: <%s>", data) } if data, err := dP.FieldAsString([]string{"complete-success-notification", "callleg", "seconds"}); err != nil { t.Error(err) } else if data != "38" { t.Errorf("expecting: 38, received: <%s>", data) } if data, err := dP.FieldAsString([]string{"complete-success-notification", "callleg[1]", "seconds"}); err != nil { t.Error(err) } else if data != "37" { t.Errorf("expecting: 37, received: <%s>", data) } if data, err := dP.FieldAsString([]string{"complete-success-notification", "callleg[@calllegid='222147']", "seconds"}); err != nil { t.Error(err) } else if data != "37" { t.Errorf("expecting: 37, received: <%s>", data) } if data, err := dP.FieldAsString([]string{"complete-success-notification", "callleg", "@calllegid"}); err != nil { t.Error(err) } else if data != "222146" { t.Errorf("expecting: 222146, received: <%s>", data) } if data, err := dP.FieldAsString([]string{"complete-success-notification", "callleg[1]", "@calllegid"}); err != nil { t.Error(err) } else if data != "222147" { t.Errorf("expecting: 222147, received: <%s>", data) } } func TestHttpXmlDPFieldAsInterface2(t *testing.T) { body := ` 2018-11-15T15:11:26 SMS smsrelay 1636488 447440935378 1632715 447440935378 0.0000 0.0360 2774 447440935378 0.0000 0.0135 447930323266 0.0000 0.0225 ` req, err := http.NewRequest("POST", "http://localhost:8080/", bytes.NewBuffer([]byte(body))) if err != nil { t.Error(err) } dP, _ := newHTTPXmlDP(req) if data, err := dP.FieldAsString([]string{"sms-notification", "callleg", "agentcost", "@amount"}); err != nil { t.Error(err) } else if data != "0.0135" { t.Errorf("expecting: 0.0135, received: <%s>", data) } if data, err := dP.FieldAsString([]string{"sms-notification", "callleg[0]", "agentcost"}); err != nil { t.Error(err) } else if data != "0.0135" { t.Errorf("expecting: 0.0135, received: <%s>", data) } if data, err := dP.FieldAsString([]string{"sms-notification", "callleg[1]", "agentcost"}); err != nil { t.Error(err) } else if data != "0.0225" { t.Errorf("expecting: 0.0225, received: <%s>", data) } }