Initial commit

This commit is contained in:
zhiqiang feng
2020-05-11 14:58:22 +08:00
commit cf86eadf63
6 changed files with 434 additions and 0 deletions
+132
View File
@@ -0,0 +1,132 @@
package internal
import (
"bytes"
"encoding/xml"
"regexp"
"strconv"
"strings"
)
const (
sendtpl = `<?xml version="1.0" encoding="UTF-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<soap:Header>
<BHIA_CIIMS:AuthenticationToken xmlns:BHIA_CIIMS="http://ciims.bhia.itdcl.com/ExchangeService">
<BHIA_CIIMS:Username xmlns:BHIA_CIIMS="http://ciims.bhia.itdcl.com/ExchangeService">##user##</BHIA_CIIMS:Username>
<BHIA_CIIMS:Password xmlns:BHIA_CIIMS="http://ciims.bhia.itdcl.com/ExchangeService">##pass##</BHIA_CIIMS:Password>
</BHIA_CIIMS:AuthenticationToken>
</soap:Header>
<soap:Body>
<ns1:send xmlns:ns1="http://ciims.bhia.itdcl.com/ExchangeService">
<ns1:in0>##msg##</ns1:in0>
<ns2:in1 xmlns="http://msg.ciims.bhia.itdcl.com" xmlns:ns2="http://ciims.bhia.itdcl.com/ExchangeService">
<ack>false</ack>
<priority>##priority##</priority>
<routeId>##event##</routeId>
<valXml>##xml##</valXml>
</ns2:in1>
</ns1:send>
</soap:Body>
</soap:Envelope>
`
receivetpl = `<?xml version="1.0" encoding="UTF-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<soap:Header>
<BHIA_CIIMS:AuthenticationToken xmlns:BHIA_CIIMS="http://ciims.bhia.itdcl.com/ExchangeService">
<BHIA_CIIMS:Username xmlns:BHIA_CIIMS="http://ciims.bhia.itdcl.com/ExchangeService">##user##</BHIA_CIIMS:Username>
<BHIA_CIIMS:Password xmlns:BHIA_CIIMS="http://ciims.bhia.itdcl.com/ExchangeService">##pass##</BHIA_CIIMS:Password>
</BHIA_CIIMS:AuthenticationToken>
</soap:Header>
<soap:Body>
<ns1:receive xmlns:ns1="http://ciims.bhia.itdcl.com/ExchangeService">
<ns1:in0>##count##</ns1:in0>
</ns1:receive>
</soap:Body>
</soap:Envelope>
`
msgExp = `<ns1:string>.*<\/ns1:string>`
)
// CreateSend xml string for sending
func CreateSend(user string, pass string, priority int, event string, valXML bool, message string) string {
var mb bytes.Buffer
xml.Escape(&mb, []byte(message))
msg := strings.Replace(sendtpl, "##user##", user, -1)
msg = strings.Replace(msg, "##pass##", pass, -1)
msg = strings.Replace(msg, "##event##", event, -1)
msg = strings.Replace(msg, "##priority##", strconv.Itoa(priority), -1)
msg = strings.Replace(msg, "##xml##", strconv.FormatBool(valXML), -1)
msg = strings.Replace(msg, "##msg##", mb.String(), -1)
return msg
}
//CreateReceive message
func CreateReceive(user string, pass string, count int) string {
msg := strings.Replace(receivetpl, "##user##", user, -1)
msg = strings.Replace(msg, "##pass##", pass, -1)
msg = strings.Replace(msg, "##count##", strconv.Itoa(count), -1)
return msg
}
func Map(vs []string, f func(string) string) []string {
vsm := make([]string, len(vs))
for i, v := range vs {
vsm[i] = f(v)
}
return vsm
}
//GetMsgs get message from soap response
func GetMsgs(soap string) []string {
r, _ := regexp.Compile(msgExp)
msgs := r.FindAllString(soap, -1)
return Map(msgs, split)
}
func split(msg string) string {
if len(msg) > 12 {
return msg[12 : len(msg)-13]
} else {
return msg
}
}
+120
View File
@@ -0,0 +1,120 @@
package internal
import (
"testing"
"github.com/stretchr/testify/assert"
)
const (
SendEsp = `<?xml version="1.0" encoding="UTF-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<soap:Header>
<BHIA_CIIMS:AuthenticationToken xmlns:BHIA_CIIMS="http://ciims.bhia.itdcl.com/ExchangeService">
<BHIA_CIIMS:Username xmlns:BHIA_CIIMS="http://ciims.bhia.itdcl.com/ExchangeService">FIMS</BHIA_CIIMS:Username>
<BHIA_CIIMS:Password xmlns:BHIA_CIIMS="http://ciims.bhia.itdcl.com/ExchangeService">FIMSPASS</BHIA_CIIMS:Password>
</BHIA_CIIMS:AuthenticationToken>
</soap:Header>
<soap:Body>
<ns1:send xmlns:ns1="http://ciims.bhia.itdcl.com/ExchangeService">
<ns1:in0>&lt;MSG/&gt;</ns1:in0>
<ns2:in1 xmlns="http://msg.ciims.bhia.itdcl.com" xmlns:ns2="http://ciims.bhia.itdcl.com/ExchangeService">
<ack>false</ack>
<priority>1</priority>
<routeId>FLOP-CHDT-ORMS-FIMS</routeId>
<valXml>false</valXml>
</ns2:in1>
</ns1:send>
</soap:Body>
</soap:Envelope>
`
ReceiveEsp = `<?xml version="1.0" encoding="UTF-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<soap:Header>
<BHIA_CIIMS:AuthenticationToken xmlns:BHIA_CIIMS="http://ciims.bhia.itdcl.com/ExchangeService">
<BHIA_CIIMS:Username xmlns:BHIA_CIIMS="http://ciims.bhia.itdcl.com/ExchangeService">FIMS</BHIA_CIIMS:Username>
<BHIA_CIIMS:Password xmlns:BHIA_CIIMS="http://ciims.bhia.itdcl.com/ExchangeService">FIMSPASS</BHIA_CIIMS:Password>
</BHIA_CIIMS:AuthenticationToken>
</soap:Header>
<soap:Body>
<ns1:receive xmlns:ns1="http://ciims.bhia.itdcl.com/ExchangeService">
<ns1:in0>2</ns1:in0>
</ns1:receive>
</soap:Body>
</soap:Envelope>
`
ReceiveResp string = `<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<soap:Body>
<ns1:receiveResponse xmlns:ns1="http://ciims.bhia.itdcl.com/ExchangeService">
<ns1:out>
<ns1:string>&lt;?xml version="1.0" encoding="UTF-8"?> &lt;msg xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="unisysaodbsis.xsd"> &lt;meta> &lt;sndr>AODB&lt;/sndr> &lt;seqn>820830&lt;/seqn> &lt;dttm>20180527124442&lt;/dttm> &lt;type>FLOP&lt;/type> &lt;styp>ESTT&lt;/styp> &lt;/meta> &lt;flop> &lt;flid>10725849&lt;/flid> &lt;ffid>CA-CA4242-A-27MAY181955-D&lt;/ffid> &lt;estt>27MAY181945&lt;/estt> &lt;/flop> &lt;/msg></ns1:string>
<ns1:string>&lt;?xml version="1.0" encoding="UTF-8"?> &lt;msg xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="unisysaodbsis.xsd"> &lt;meta> &lt;sndr>AODB&lt;/sndr> &lt;seqn>820830&lt;/seqn> &lt;dttm>20180527124442&lt;/dttm> &lt;type>FLOP&lt;/type> &lt;styp>ESTT&lt;/styp> &lt;/meta> &lt;flop> &lt;flid>10725849&lt;/flid> &lt;ffid>CA-CA4242-A-27MAY181955-D&lt;/ffid> &lt;estt>27MAY181945&lt;/estt> &lt;/flop> &lt;/msg></ns1:string>
</ns1:out>
</ns1:receiveResponse>
</soap:Body>
</soap:Envelope>`
Msg string = `<?xml version="1.0" encoding="UTF-8"?> <msg xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="unisysaodbsis.xsd"> <meta> <sndr>AODB</sndr> <seqn>820830</seqn> <dttm>20180527124442</dttm> <type>FLOP</type> <styp>ESTT</styp> </meta> <flop> <flid>10725849</flid> <ffid>CA-CA4242-A-27MAY181955-D</ffid> <estt>27MAY181945</estt> </flop> </msg>`
)
func TestSend(t *testing.T) {
assert := assert.New(t)
// assert equality
// assert.Equal(123, 123, "they should be equal")
msg := CreateSend("FIMS", "FIMSPASS", 1, "FLOP-CHDT-ORMS-FIMS", false, "<MSG/>")
// println(msg)
assert.Equal(SendEsp, msg, "send post should be equal")
}
func TestReceive(t *testing.T) {
assert := assert.New(t)
msg := CreateReceive("FIMS", "FIMSPASS", 2)
assert.Equal(ReceiveEsp, msg, "receive post should be equal")
}
func TestGetMsg(t *testing.T) {
assert := assert.New(t)
msgs := GetMsgs(ReceiveResp)
assert.Equal(2, len(msgs), "should get 2 message")
assert.Equal(Msg, msgs[0], "message")
}
+49
View File
@@ -0,0 +1,49 @@
package internal
import (
"bytes"
"io/ioutil"
"net/http"
"strings"
"time"
)
const (
defaultContentType string = "text/xml; charset=UTF-8"
defaultAgent string = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0; XFire Client +http://xfire.codehaus.org)"
)
//Post xml string to url
func post(url string, contentType string, msg string) (*http.Response, error) {
return http.Post(url, contentType, bytes.NewBuffer([]byte(msg)))
}
func postSim(url string, msg string) string {
timeout := time.Duration(10 * time.Second)
client := http.Client{
Timeout: timeout,
}
req, err := http.NewRequest(http.MethodPost, url, strings.NewReader(msg))
if err != nil {
return err.Error()
}
req.Header.Set("Content-Type", defaultContentType)
req.Header.Set("User-Agent", defaultAgent)
req.Header.Set("SOAPAction", "")
resp, err := client.Do(req)
if err != nil {
return err.Error()
}
defer resp.Body.Close()
respBytes, err := ioutil.ReadAll(resp.Body)
if err != nil {
return err.Error()
}
return string(respBytes)
}
//Send message
func Send(url string, message string) string {
return postSim(url, message)
}