add tests for notification server errors (#30)

This commit is contained in:
Kirill
2023-07-29 02:59:03 +03:00
committed by GitHub
parent 6c1a5e35c0
commit 651c39595f
2 changed files with 139 additions and 10 deletions

View File

@@ -53,12 +53,7 @@ func (ns *NotificationServer) handler(rw http.ResponseWriter, httpReq *http.Requ
Name: "Ozon Seller API",
Time: time.Now(),
}
respJson, err := json.Marshal(resp)
if err != nil {
log.Print(err)
ns.error(rw, http.StatusInternalServerError, err)
return
}
respJson, _ := json.Marshal(resp)
rw.WriteHeader(http.StatusOK)
rw.Write(respJson)
@@ -68,8 +63,7 @@ func (ns *NotificationServer) handler(rw http.ResponseWriter, httpReq *http.Requ
req, err := ns.unmarshal(mt.MessageType, content)
if err != nil {
log.Print(err)
ns.result(rw, false)
//ns.error(rw, http.StatusInternalServerError, err)
ns.error(rw, http.StatusInternalServerError, err)
return
}
h, ok := ns.handlers[mt.MessageType]
@@ -80,8 +74,7 @@ func (ns *NotificationServer) handler(rw http.ResponseWriter, httpReq *http.Requ
}
if err := h(req); err != nil {
log.Print(err)
ns.result(rw, false)
//ns.error(rw, http.StatusInternalServerError, err)
ns.result(rw, true)
return
}

View File

@@ -505,10 +505,146 @@ func TestNotificationServer(t *testing.T) {
err = json.Unmarshal(gotJson, &got)
if err != nil {
t.Error(err)
continue
}
err = json.Unmarshal([]byte(testCase.response), &expected)
if err != nil {
t.Error(err)
continue
}
if err := compare(expected, got); err != nil {
t.Error(err)
continue
}
}
}
func TestNotificationServerErrors(t *testing.T) {
testCases := []struct {
request testData
response string
}{
{
testData{
raw: `{
"message_type": "string"
}`,
},
`
{
"error": {
"code": "500",
"message": "unsupported type: string",
"details": ""
}
}`,
},
{
testData{
raw: `invalid json`,
},
`{
"error": {
"code": "400",
"message": "invalid character 'i' looking for beginning of value",
"details": ""
}
}`,
},
{
testData{
raw: `{
"message_type": "TYPE_NEW_POSTING",
"field": [[
}`,
},
`{
"error": {
"code": "400",
"message": "invalid character '}' looking for beginning of value",
"details": ""
}
}`,
},
{
testData{
raw: `{
"message_type": "TYPE_NEW_POSTING"
}`,
},
`{
"result": true
}`,
},
{
testData{
raw: `{
"message_type": "TYPE_PING",
"time": "2019-08-24T14:15:22Z",
}`,
},
`{
"error": {
"code": "400",
"message": "invalid character '}' looking for beginning of object key string",
"details": ""
}
}`,
},
{
testData{
raw: `{
"message_type": "TYPE_CHAT_CLOSED"
}`,
},
`{
"result": true
}`,
},
}
port := getFreePort()
client := http.Client{}
server := NewNotificationServer(port)
server.Register(NewPostingType, func(req interface{}) error {
return fmt.Errorf("just error")
})
go func() {
if err := server.Run(); err != nil {
t.Fatalf("notification server is down: %s", err)
}
}()
// TODO: get rid of it
// Needed to make sure server is running
time.Sleep(3 * time.Second)
for _, testCase := range testCases {
httpResp, err := client.Post(fmt.Sprintf("http://0.0.0.0:%d/", port), "application/json", strings.NewReader(testCase.request.raw))
if err != nil {
t.Error(err)
continue
}
gotJson, err := ioutil.ReadAll(httpResp.Body)
if err != nil {
t.Error(err)
continue
}
expected := map[string]interface{}{}
got := map[string]interface{}{}
err = json.Unmarshal(gotJson, &got)
if err != nil {
t.Error(err)
continue
}
err = json.Unmarshal([]byte(testCase.response), &expected)
if err != nil {
t.Error(err)
continue
}
if err := compare(expected, got); err != nil {