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