github.com/fastwego/offiaccount@v1.0.1/server_test.go (about) 1 // Copyright 2020 FastWeGo 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package offiaccount 16 17 import ( 18 "encoding/xml" 19 "fmt" 20 "net/http" 21 "net/http/httptest" 22 "net/url" 23 "os" 24 "reflect" 25 "testing" 26 27 "github.com/fastwego/offiaccount/type/type_event" 28 29 "github.com/fastwego/offiaccount/type/type_message" 30 ) 31 32 var MockOffiAccount *OffiAccount 33 34 func TestMain(m *testing.M) { 35 MockOffiAccount = New(Config{ 36 Appid: "TestClient_getAccessToken", 37 Secret: "SECRET", 38 }) 39 os.Exit(m.Run()) 40 } 41 42 func TestServer_ParseXML(t *testing.T) { 43 44 type args struct { 45 body []byte 46 } 47 tests := []struct { 48 name string 49 args args 50 wantM interface{} 51 wantErr bool 52 }{ 53 { 54 name: "case1", 55 args: args{body: []byte(` 56 <xml> 57 <ToUserName><![CDATA[toUser]]></ToUserName> 58 <FromUserName><![CDATA[fromUser]]></FromUserName> 59 <CreateTime>1348831860</CreateTime> 60 <MsgType><![CDATA[text]]></MsgType> 61 <Content><![CDATA[this is a test]]></Content> 62 <MsgId>1234567890123456</MsgId> 63 </xml> 64 `)}, 65 wantM: type_message.MessageText{ 66 Message: type_message.Message{ 67 ToUserName: "toUser", 68 FromUserName: "fromUser", 69 CreateTime: "1348831860", 70 MsgType: "text", 71 }, 72 MsgId: "1234567890123456", 73 Content: "this is a test", 74 }, 75 wantErr: false, 76 }, 77 { 78 name: "case2", 79 args: args{body: []byte(` 80 <xml> 81 <ToUserName><![CDATA[gh_7f083739789a]]></ToUserName> 82 <FromUserName><![CDATA[oia2TjuEGTNoeX76QEjQNrcURxG8]]></FromUserName> 83 <CreateTime>1395658920</CreateTime> 84 <MsgType><![CDATA[event]]></MsgType> 85 <Event><![CDATA[TEMPLATESENDJOBFINISH]]></Event> 86 <MsgID>200163836</MsgID> 87 <Status><![CDATA[success]]></Status> 88 </xml> 89 `)}, 90 wantM: type_event.EventTemplateSendJobFinish{ 91 Event: type_event.Event{ 92 Message: type_message.Message{ 93 ToUserName: "gh_7f083739789a", 94 FromUserName: "oia2TjuEGTNoeX76QEjQNrcURxG8", 95 CreateTime: "1395658920", 96 MsgType: "event", 97 }, 98 Event: "TEMPLATESENDJOBFINISH", 99 }, 100 MsgID: "200163836", 101 Status: "success", 102 }, 103 wantErr: false, 104 }, 105 } 106 107 for _, tt := range tests { 108 t.Run(tt.name, func(t *testing.T) { 109 gotM, err := MockOffiAccount.Server.ParseXML(tt.args.body) 110 if (err != nil) != tt.wantErr { 111 t.Errorf("ParseXML() error = %v, wantErr %v", err, tt.wantErr) 112 return 113 } 114 if !reflect.DeepEqual(gotM, tt.wantM) { 115 t.Errorf("ParseXML() gotM = %v, want %v", gotM, tt.wantM) 116 } 117 }) 118 } 119 } 120 121 func TestReplyMessage(t *testing.T) { 122 tests := []struct { 123 name string 124 wantEcho string 125 }{ 126 { 127 name: "case1", 128 wantEcho: `<xml><ToUserName><![CDATA[toUser]]></ToUserName><FromUserName><![CDATA[fromUser]]></FromUserName><CreateTime>12345678</CreateTime><MsgType><![CDATA[text]]></MsgType><Content><![CDATA[你好]]></Content></xml>`, 129 }, 130 } 131 for _, tt := range tests { 132 t.Run(tt.name, func(t *testing.T) { 133 // 回复文本消息 134 msg := type_message.ReplyMessageText{ 135 ReplyMessage: type_message.ReplyMessage{ 136 ToUserName: "toUser", 137 FromUserName: "fromUser", 138 CreateTime: "12345678", 139 MsgType: type_message.ReplyMsgTypeText, 140 }, 141 Content: "你好", 142 } 143 data, err := xml.Marshal(msg) 144 fmt.Println(string(data), err) 145 146 if tt.wantEcho != string(data) { 147 t.Errorf("\nwant %s \nget %s", tt.wantEcho, string(data)) 148 } 149 }) 150 } 151 } 152 153 func TestServer_EchoStr(t *testing.T) { 154 MockOffiAccount := New(Config{ 155 Token: "TOKEN", 156 }) 157 s := &Server{ 158 Ctx: MockOffiAccount, 159 } 160 161 // Mock Server 162 MockSvrHandler := http.NewServeMux() 163 MockSvrHandler.HandleFunc("/echostr", s.EchoStr) 164 165 tests := []struct { 166 name string 167 args url.Values 168 wantEcho string 169 }{ 170 { 171 name: "case1", 172 args: url.Values{ 173 "timestamp": []string{"1526545852"}, 174 "nonce": []string{"nonce"}, 175 "echostr": []string{"echostr"}, 176 "signature": []string{"7aa016688a328036de9ea9164ee00f9fa581da5f"}, 177 }, 178 wantEcho: "echostr", 179 }, 180 { 181 name: "case2", 182 args: url.Values{ 183 "timestamp": []string{"1526545852"}, 184 "nonce": []string{"nonce"}, 185 "echostr": []string{"echostr"}, 186 "signature": []string{"123"}, 187 }, 188 wantEcho: "", 189 }, 190 } 191 192 for _, tt := range tests { 193 t.Run(tt.name, func(t *testing.T) { 194 r, _ := http.NewRequest(http.MethodGet, "/echostr?"+tt.args.Encode(), nil) 195 w := httptest.NewRecorder() 196 MockSvrHandler.ServeHTTP(w, r) 197 resp := w.Result() 198 if resp.StatusCode != http.StatusOK { 199 t.Errorf("Response code is %v", resp.StatusCode) 200 } 201 202 echo := string(w.Body.Bytes()) 203 fmt.Println(echo) 204 205 if tt.wantEcho != echo { 206 t.Errorf("want %s but get %s", tt.wantEcho, echo) 207 } 208 }) 209 } 210 }