Add tests for *text_plain reply for HTTPAgent

This commit is contained in:
TeoV
2019-07-09 13:20:25 +03:00
committed by Dan Christian Bogos
parent 524f726256
commit fc45b4f7b9
5 changed files with 45 additions and 32 deletions

View File

@@ -313,22 +313,21 @@ func testHAitTextPlain(t *testing.T) {
addr = haCfg.ListenCfg().HTTPTLSListen
httpConst = "https"
}
reqUrl := fmt.Sprintf("%s://%s%s?request_type=TextPlainDryRun&CallID=123456&Msisdn=497700056231&Imsi=2343000000000123&Destination=491239440004&MSRN=0102220233444488999&ProfileID=1&AgentID=176&GlobalMSISDN=497700056129&GlobalIMSI=214180000175129&ICCID=8923418450000089629&MCC=234&MNC=10&calltype=callback",
reqUrl := fmt.Sprintf("%s://%s%s?request_type=TextPlainDryRun&CallID=123456&Msisdn=497700056231&Imsi=2343000000000123&Destination=491239440004",
httpConst, addr, haCfg.HttpAgentCfg()[2].Url)
rply, err := httpC.Get(reqUrl)
if err != nil {
t.Fatal(err)
}
eXml := []byte(`<?xml version="1.0" encoding="UTF-8"?>
<response>
<Allow>1</Allow>
<Concatenated>234/Val1</Concatenated>
<MaxDuration>1200</MaxDuration>
</response>`)
response := []byte(`Variable1=Hola1
Variable2=Hola2
ComposedVar=TestComposed
Item1.1=Val2
`)
if body, err := ioutil.ReadAll(rply.Body); err != nil {
t.Error(err)
} else if !reflect.DeepEqual(eXml, body) {
t.Errorf("expecting: <%s>, received: <%s>", string(eXml), string(body))
} else if !reflect.DeepEqual(response, body) {
t.Errorf("expecting: \n<%s>\n, received: \n<%s>\n", string(response), string(body))
}
rply.Body.Close()
time.Sleep(time.Millisecond)

View File

@@ -20,6 +20,7 @@ package agents
import (
"encoding/xml"
"errors"
"fmt"
"io/ioutil"
"net"
@@ -257,14 +258,23 @@ type haTextPlainEncoder struct {
// Encode implements httpAgentReplyEncoder
func (xE *haTextPlainEncoder) Encode(nM *config.NavigableMap) (err error) {
var str string
msgFields := make(map[string]string) // work around to NMap issue
for _, val := range nM.Values() {
nmItms, isNMItems := val.([]*config.NMItem)
if !isNMItems {
return fmt.Errorf("value: %+v is not []*NMItem", val)
}
for _, nmItm := range nmItms {
str += fmt.Sprintf("%s=%s\n", strings.Join(nmItm.Path, utils.NestingSep), utils.IfaceAsString(nmItm.Data))
if len(nmItms) == 0 {
continue
}
cfgItm := nmItms[0] // first item gives some config for the rest, cannot iterate through NMItems since they are multipled by order
if len(cfgItm.Path) == 0 {
return errors.New("empty path in config item")
}
msgFields[strings.Join(cfgItm.Path, utils.NestingSep)] = utils.IfaceAsString(cfgItm.Data)
}
for key, val := range msgFields {
str += fmt.Sprintf("%s=%s\n", key, val)
}
_, err = xE.w.Write([]byte(str))
return

View File

@@ -632,7 +632,7 @@ func (self *CGRConfig) checkConfigSanity() error {
return fmt.Errorf("<%s> unsupported request payload %s",
utils.HTTPAgent, httpAgentCfg.RequestPayload)
}
if !utils.IsSliceMember([]string{utils.MetaXml}, httpAgentCfg.ReplyPayload) {
if !utils.IsSliceMember([]string{utils.MetaXml, utils.MetaTextPlain}, httpAgentCfg.ReplyPayload) {
return fmt.Errorf("<%s> unsupported reply payload %s",
utils.HTTPAgent, httpAgentCfg.ReplyPayload)
}

View File

@@ -142,16 +142,18 @@
"request_fields":[
],
"reply_fields":[
{"tag": "Allow", "field_id": "response.Allow", "type": "*constant",
"value": "1", "mandatory": true},
{"tag": "Concatenated1", "field_id": "response.Concatenated", "type": "*composed",
"value": "~*req.MCC;/", "mandatory": true},
{"tag": "Concatenated2", "field_id": "response.Concatenated", "type": "*composed",
"value": "Val1"},
{"tag": "MaxDuration", "field_id": "response.MaxDuration", "type": "*constant",
"value": "1200", "blocker": true},
{"tag": "Unused", "field_id": "response.Unused", "type": "*constant",
"value": "0"},
{"tag": "Field1", "field_id": "Variable1", "type": "*variable",
"value": "Hola1", "mandatory": true},
{"tag": "Field2", "field_id": "Variable2", "type": "*variable",
"value": "Hola2", "mandatory": true},
{"tag": "Field3", "field_id": "ComposedVar", "type": "*composed",
"value": "Test", "mandatory": true},
{"tag": "Field4", "field_id": "ComposedVar", "type": "*composed",
"value": "Composed", "mandatory": true},
{"tag": "Field5", "field_id": "Item1.1", "type": "*constant",
"value": "Val1", "mandatory": true},
{"tag": "Field6", "field_id": "Item1.1", "type": "*constant",
"value": "Val2", "mandatory": true},
],
},
],

View File

@@ -142,16 +142,18 @@
"request_fields":[
],
"reply_fields":[
{"tag": "Allow", "field_id": "response.Allow", "type": "*constant",
"value": "1", "mandatory": true},
{"tag": "Concatenated1", "field_id": "response.Concatenated", "type": "*composed",
"value": "~*req.MCC;/", "mandatory": true},
{"tag": "Concatenated2", "field_id": "response.Concatenated", "type": "*composed",
"value": "Val1"},
{"tag": "MaxDuration", "field_id": "response.MaxDuration", "type": "*constant",
"value": "1200", "blocker": true},
{"tag": "Unused", "field_id": "response.Unused", "type": "*constant",
"value": "0"},
{"tag": "Field1", "field_id": "Variable1", "type": "*variable",
"value": "Hola1", "mandatory": true},
{"tag": "Field2", "field_id": "Variable2", "type": "*variable",
"value": "Hola2", "mandatory": true},
{"tag": "Field3", "field_id": "ComposedVar", "type": "*composed",
"value": "Test", "mandatory": true},
{"tag": "Field4", "field_id": "ComposedVar", "type": "*composed",
"value": "Composed", "mandatory": true},
{"tag": "Field5", "field_id": "Item1.1", "type": "*constant",
"value": "Val1", "mandatory": true},
{"tag": "Field6", "field_id": "Item1.1", "type": "*constant",
"value": "Val2", "mandatory": true},
],
},
],