fix tests
This commit is contained in:
@@ -4,6 +4,7 @@ import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"net/http"
|
||||
"time"
|
||||
)
|
||||
@@ -36,10 +37,12 @@ func (ns *NotificationServer) handler(rw http.ResponseWriter, httpReq *http.Requ
|
||||
mt := &Common{}
|
||||
content, err := ioutil.ReadAll(httpReq.Body)
|
||||
if err != nil {
|
||||
log.Print(err)
|
||||
ns.error(rw, http.StatusBadRequest, err)
|
||||
return
|
||||
}
|
||||
if err := json.Unmarshal(content, mt); err != nil {
|
||||
log.Print(err)
|
||||
ns.error(rw, http.StatusBadRequest, err)
|
||||
return
|
||||
}
|
||||
@@ -52,6 +55,7 @@ func (ns *NotificationServer) handler(rw http.ResponseWriter, httpReq *http.Requ
|
||||
}
|
||||
respJson, err := json.Marshal(resp)
|
||||
if err != nil {
|
||||
log.Print(err)
|
||||
ns.error(rw, http.StatusInternalServerError, err)
|
||||
return
|
||||
}
|
||||
@@ -63,16 +67,25 @@ 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)
|
||||
return
|
||||
}
|
||||
h := ns.handlers[mt.MessageType]
|
||||
h, ok := ns.handlers[mt.MessageType]
|
||||
if !ok {
|
||||
ns.result(rw, true)
|
||||
log.Printf("handler for %s is not registered", mt.MessageType)
|
||||
return
|
||||
}
|
||||
if err := h(req); err != nil {
|
||||
log.Print(err)
|
||||
ns.result(rw, false)
|
||||
//ns.error(rw, http.StatusInternalServerError, err)
|
||||
return
|
||||
}
|
||||
|
||||
ns.result(rw, true)
|
||||
}
|
||||
|
||||
func (ns *NotificationServer) Register(mt MessageType, handler func(req interface{}) error) {
|
||||
|
||||
@@ -4,13 +4,12 @@ import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"net"
|
||||
"net/http"
|
||||
"reflect"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
const (
|
||||
port = 5000
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestNotificationServer(t *testing.T) {
|
||||
@@ -21,7 +20,7 @@ func TestNotificationServer(t *testing.T) {
|
||||
// PING
|
||||
{
|
||||
`{
|
||||
"message_type": "string",
|
||||
"message_type": "TYPE_PING",
|
||||
"time": "2019-08-24T14:15:22Z"
|
||||
}`,
|
||||
`{
|
||||
@@ -61,11 +60,13 @@ func TestNotificationServer(t *testing.T) {
|
||||
"seller_id": "7"
|
||||
}`,
|
||||
`{
|
||||
"result": false
|
||||
"result": true
|
||||
}`,
|
||||
},
|
||||
}
|
||||
|
||||
port := getFreePort()
|
||||
|
||||
client := http.Client{}
|
||||
server := NewNotificationServer(port)
|
||||
server.Register(ChatClosedType, func(req interface{}) error {
|
||||
@@ -81,8 +82,12 @@ func TestNotificationServer(t *testing.T) {
|
||||
}
|
||||
}()
|
||||
|
||||
// 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://127.0.0.1:%d/", port), "application/json", strings.NewReader(testCase.request))
|
||||
httpResp, err := client.Post(fmt.Sprintf("http://0.0.0.0:%d/", port), "application/json", strings.NewReader(testCase.request))
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
continue
|
||||
@@ -94,10 +99,13 @@ func TestNotificationServer(t *testing.T) {
|
||||
continue
|
||||
}
|
||||
|
||||
expected := map[string]string{}
|
||||
got := map[string]string{}
|
||||
json.Unmarshal(gotJson, got)
|
||||
json.Unmarshal([]byte(testCase.response), expected)
|
||||
expected := map[string]interface{}{}
|
||||
got := map[string]interface{}{}
|
||||
err = json.Unmarshal(gotJson, &got)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
err = json.Unmarshal([]byte(testCase.response), &expected)
|
||||
|
||||
if err := compare(expected, got); err != nil {
|
||||
t.Error(err)
|
||||
@@ -106,13 +114,20 @@ func TestNotificationServer(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func compare(expected map[string]string, got map[string]string) error {
|
||||
func compare(expected map[string]interface{}, got map[string]interface{}) error {
|
||||
for k, v := range expected {
|
||||
if gotValue, ok := got[k]; !ok {
|
||||
return fmt.Errorf("key %s is expected to present", k)
|
||||
} else if gotValue != v {
|
||||
return fmt.Errorf("key %s is not equal, got: %s, want: %s", k, gotValue, v)
|
||||
} else if !reflect.DeepEqual(gotValue, v) {
|
||||
return fmt.Errorf("key %s is not equal, got: %v, want: %v", k, gotValue, v)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func getFreePort() int {
|
||||
listener, _ := net.Listen("tcp", ":0")
|
||||
defer listener.Close()
|
||||
|
||||
return listener.Addr().(*net.TCPAddr).Port
|
||||
}
|
||||
|
||||
@@ -42,7 +42,7 @@ type NewPosting struct {
|
||||
WarehouseId int64 `json:"warehouse_id"`
|
||||
|
||||
// Seller identifier
|
||||
SellerId int64 `json:"seller_id"`
|
||||
SellerId string `json:"seller_id"`
|
||||
}
|
||||
|
||||
type Product struct {
|
||||
@@ -79,7 +79,7 @@ type PostingCancelled struct {
|
||||
WarehouseId int64 `json:"warehouse_id"`
|
||||
|
||||
// Seller identifier
|
||||
SellerId int64 `json:"seller_id"`
|
||||
SellerId string `json:"seller_id"`
|
||||
}
|
||||
|
||||
type Reason struct {
|
||||
@@ -107,7 +107,7 @@ type StateChanged struct {
|
||||
WarehouseId int64 `json:"warehouse_id"`
|
||||
|
||||
// Seller identifier
|
||||
SellerId int64 `json:"seller_id"`
|
||||
SellerId string `json:"seller_id"`
|
||||
}
|
||||
|
||||
// Shipment shipping date change
|
||||
@@ -127,7 +127,7 @@ type CutoffDateChanged struct {
|
||||
WarehouseId int64 `json:"warehouse_id"`
|
||||
|
||||
// Seller identifier
|
||||
SellerId int64 `json:"seller_id"`
|
||||
SellerId string `json:"seller_id"`
|
||||
}
|
||||
|
||||
// Shipment delivery date change
|
||||
@@ -153,7 +153,7 @@ type DeliveryDateChanged struct {
|
||||
WarehouseId int64 `json:"warehouse_id"`
|
||||
|
||||
// Seller identifier
|
||||
SellerId int64 `json:"seller_id"`
|
||||
SellerId string `json:"seller_id"`
|
||||
}
|
||||
|
||||
// Product creation and update or processing error
|
||||
@@ -173,7 +173,7 @@ type CreateOrUpdateItem struct {
|
||||
ChangedAt time.Time `json:"changed_at"`
|
||||
|
||||
// Seller identifier
|
||||
SellerId int64 `json:"seller_id"`
|
||||
SellerId string `json:"seller_id"`
|
||||
}
|
||||
|
||||
// Product price index change
|
||||
@@ -193,7 +193,7 @@ type PriceIndexChanged struct {
|
||||
PriceIndex int64 `json:"price_index"`
|
||||
|
||||
// Seller identifier
|
||||
SellerId int64 `json:"seller_id"`
|
||||
SellerId string `json:"seller_id"`
|
||||
}
|
||||
|
||||
// Stock change at the seller's warehouse
|
||||
@@ -204,7 +204,7 @@ type StocksChanged struct {
|
||||
Items []Item `json:"items"`
|
||||
|
||||
// Seller identifier
|
||||
SellerId int64 `json:"seller_id"`
|
||||
SellerId string `json:"seller_id"`
|
||||
}
|
||||
|
||||
type Item struct {
|
||||
@@ -255,7 +255,7 @@ type NewMessage struct {
|
||||
Data []string `json:"data"`
|
||||
|
||||
// Seller identifier
|
||||
SellerId int64 `json:"seller_id"`
|
||||
SellerId string `json:"seller_id"`
|
||||
}
|
||||
|
||||
type User struct {
|
||||
@@ -302,7 +302,7 @@ type ChatClosed struct {
|
||||
Type string `json:"type"`
|
||||
|
||||
// Seller identifier
|
||||
SellerId int64 `json:"seller_id"`
|
||||
SellerId string `json:"seller_id"`
|
||||
}
|
||||
|
||||
type Response struct {
|
||||
|
||||
Reference in New Issue
Block a user