Browse Source

Adding tests in go

main
Дмитрий 3 years ago
parent
commit
b023f0d3f1
  1. 30
      Makefile
  2. 22
      cmd/client_server/validators.go
  3. 82
      cmd/client_server/validators_test.go
  4. 29
      internal/json/valid_response.json

30
Makefile

@ -4,6 +4,7 @@ moc_server_address := 127.0.0.1:5059
bold := \033[1m bold := \033[1m
normal := \033[0m normal := \033[0m
good := \033[1m\033[0;32m
help: help:
@echo "$(bold)Makefile commands$(normal)" @echo "$(bold)Makefile commands$(normal)"
@ -21,35 +22,48 @@ run:
go run cmd/main.go -p $(port) -d "$(moc_server_address)" go run cmd/main.go -p $(port) -d "$(moc_server_address)"
test-ip: test-ip:
go run cmd/main.go -p $(port) -d "$(moc_server_address),localhost:5059" @echo
@go run cmd/main.go -p $(port) -d "$(moc_server_address),localhost:5059" || \
{ echo "\n[+] PASS wrong IP address test"; exit 0; }
test-port: test-port:
go run cmd/main.go -p $(port) -d "$(moc_server_address),127.0.0.1:as" @echo
@go run cmd/main.go -p $(port) -d "$(moc_server_address),127.0.0.1:as" || \
{ echo "\n[+] PASS wrong port test"; exit 0; }
test-port-max: test-port-max:
go run cmd/main.go -p $(port) -d "$(moc_server_address),127.0.0.1:65537" @echo
@go run cmd/main.go -p $(port) -d "$(moc_server_address),127.0.0.1:65537" || \
{ echo "\n[+] PASS port too big test"; exit 0; }
test-port-endpoint: test-port-endpoint:
go run cmd/main.go -p $(port) -d "127.0.0.1:9001/bid_request" @echo
@go run cmd/main.go -p $(port) -d "127.0.0.1:9001/bid_request" || \
{ echo "\n[+] PASS endpoint with address test"; exit 0; }
build: build:
go build -o bin/simple-choose-ad cmd/main.go go build -o bin/simple-choose-ad cmd/main.go
start-moc-server: start-moc-server:
@echo "[!] Starting moc server on $(moc_server_address) ..." @echo "[!] Starting up moc-server on $(moc_server_address) ..."
@go run internal/moc_server.go -l $(moc_server_address) & @go run internal/moc_server.go -l $(moc_server_address) &
stop-moc-server: stop-moc-server:
@echo "[!] Killing moc server" @echo "[!] Stopping moc-server ..."
@curl -s -o /dev/null "$(moc_server_address)/exit" & @curl -s -o /dev/null "$(moc_server_address)/exit" &
test-server: test-server:
@echo "Testing server..." @echo
@echo "Check response from moc-server "
@$(MAKE) start-moc-server @$(MAKE) start-moc-server
@cd "cmd/client_server/"; \ @cd "cmd/client_server/"; \
go test go test -v
@$(MAKE) stop-moc-server @$(MAKE) stop-moc-server
tests: tests:
# @$(MAKE) test-ip
# @$(MAKE) test-port
# @$(MAKE) test-port-max
# @$(MAKE) test-port-endpoint
@$(MAKE) test-server @$(MAKE) test-server

22
cmd/client_server/validators.go

@ -10,7 +10,10 @@ import (
"strings" "strings"
) )
const MAX_PORT_NUM = 65535 const (
MAX_PORT_NUM = 65535
MIN_PORT_NUM = 1024
)
// Returns false if ipv4 `correct`. // Returns false if ipv4 `correct`.
func wrongIPAddresFormat(ipv4 string) bool { func wrongIPAddresFormat(ipv4 string) bool {
@ -31,21 +34,34 @@ func throwHTTPError(err_text string, code int, w *http.ResponseWriter) error {
// and `8080` port. If ip or port has wrong format, returns error. // and `8080` port. If ip or port has wrong format, returns error.
func ParsePartnersAddress(ipAndPort string) (string, int64, error) { func ParsePartnersAddress(ipAndPort string) (string, int64, error) {
var err error var err error
var ip string
var port int64
iap := strings.Split(ipAndPort, ":") iap := strings.Split(ipAndPort, ":")
ip := iap[0] if len(iap) != 2 {
err = errors.New(fmt.Sprintf("Wrong partners 'ip:port' format: %v", ipAndPort))
return ip, port, err
}
ip = iap[0]
if wrongIPAddresFormat(ip) { if wrongIPAddresFormat(ip) {
err = errors.New(fmt.Sprintf("Wrong ip address format in partner ip: %v", ip)) err = errors.New(fmt.Sprintf("Wrong ip address format in partner ip: %v", ip))
} }
port, e := strconv.ParseInt(iap[1], 10, 32) port, e := strconv.ParseInt(iap[1], 10, 64)
if e != nil { if e != nil {
err = errors.New(fmt.Sprintf("Wrong port format in partner ip: %v", e)) err = errors.New(fmt.Sprintf("Wrong port format in partner ip: %v", e))
return ip, port, err
} }
if port > MAX_PORT_NUM { if port > MAX_PORT_NUM {
err = errors.New(fmt.Sprintf("Wrong port in partner ip: grater than %v", MAX_PORT_NUM)) err = errors.New(fmt.Sprintf("Wrong port in partner ip: grater than %v", MAX_PORT_NUM))
} }
if port < MIN_PORT_NUM {
err = errors.New(fmt.Sprintf("Wrong port in partner ip: %v lower than %v", port, MIN_PORT_NUM))
}
return ip, port, err return ip, port, err
} }

82
cmd/client_server/validators_test.go

@ -0,0 +1,82 @@
package clientserver
import (
"testing"
)
// Wants: 10.10.10.10:5050
// Gets : localhost:5050
func TestIPAddresFormat_DomainName(t *testing.T) {
addres := "localhost:5050"
_, _, e := ParsePartnersAddress(addres)
if e == nil {
t.Error("Must be an error, when parsing ", addres)
}
t.Log(e)
}
// Wants: 10.10.10.10:5050
// Gets : 10.10.10.10:
func TestIPAddresFormat_OnlyIpAndColon(t *testing.T) {
addres := "10.10.10.10:"
_, _, e := ParsePartnersAddress(addres)
if e == nil {
t.Error("Must be an error, when parsing ", addres)
}
t.Log(e)
}
// Wants: 10.10.10.10:5050
// Gets : 10.10.10.10
func TestIPAddresFormat_OnlyIp(t *testing.T) {
addres := "10.10.10.10"
_, _, e := ParsePartnersAddress(addres)
if e == nil {
t.Error("Must be an error, when parsing ", addres)
}
t.Log(e)
}
// Wants: 10.10.10.10:5050
// Gets : 10.10.10.10:65537
func TestIPAddresFormat_IncorrectPortValue_TooBig(t *testing.T) {
addres := "10.10.10.10:65537"
_, _, e := ParsePartnersAddress(addres)
if e == nil {
t.Error("Must be an error, when parsing ", addres)
}
t.Log(e)
}
// Wants: 10.10.10.10:5050
// Gets : 10.10.10.10:1000
func TestIPAddresFormat_IncorrectPortValue_TooSmall(t *testing.T) {
addres := "10.10.10.10:1000"
_, _, e := ParsePartnersAddress(addres)
if e == nil {
t.Error("Must be an error, when parsing ", addres)
}
t.Log(e)
}
// Wants: 10.10.10.10:5050
// Gets : 10.10.10.10:as
func TestIPAddresFormat_IncorrectPortValue_NotANumber(t *testing.T) {
addres := "10.10.10.10:as"
_, _, e := ParsePartnersAddress(addres)
if e == nil {
t.Error("Must be an error, when parsing ", addres)
}
t.Log(e)
}
// Wants: 10.10.10.10:5050
// Gets : 10.10.10.10:5050/bid_request
func TestIPAddresFormat_AddressWithEndpoint(t *testing.T) {
addres := "10.10.10.10:5050/bid_request"
_, _, e := ParsePartnersAddress(addres)
if e == nil {
t.Error("Must be an error, when parsing ", addres)
}
t.Log(e)
}

29
internal/json/valid_response.json

@ -0,0 +1,29 @@
{
"id": "123",
"imp": [
{
"id": 123,
"width": 144,
"height": 122,
"title": "example1",
"url": "example.com",
"price": 123.5
},
{
"id": 123,
"width": 155,
"height": 133,
"title": "bestoption",
"url": "bestoption.com",
"price": 143.8
},
{
"id": 123,
"width": 155,
"height": 133,
"title": "notabestoption",
"url": "notabestoption.com",
"price": 100.8
}
]
}
Loading…
Cancel
Save