Skip to content
This repository has been archived by the owner on Mar 16, 2024. It is now read-only.

Commit

Permalink
Improving code coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
rehabaam committed Nov 11, 2023
1 parent 49fd4bf commit 7098e08
Show file tree
Hide file tree
Showing 4 changed files with 150 additions and 150 deletions.
35 changes: 25 additions & 10 deletions services/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package commands
import (
"context"
"net/http"
"os"
"os/signal"
"time"

"github.com/gorilla/mux"
Expand Down Expand Up @@ -43,11 +45,9 @@ func serveImage(w http.ResponseWriter, r *http.Request) {
}

// RunServer func for running HTTP server
func configureServer() *http.Server {
func StartServer() error {

loadFileToMemory()
r := mux.NewRouter()

api := r.PathPrefix("/api/v1").Subrouter()
api.HandleFunc("/partners", getPartners).Methods(http.MethodGet)
api.HandleFunc("/deals", getDeals).Methods(http.MethodGet)
Expand All @@ -56,20 +56,35 @@ func configureServer() *http.Server {
api.HandleFunc("/faqs", getFAQs).Methods(http.MethodGet)
api.HandleFunc("/img/{name}", serveImage).Methods(http.MethodGet)

loadFileToMemory()

server = &http.Server{
Addr: ":8080",
ReadHeaderTimeout: 5 * time.Second,
Handler: r,
}
return server
}

func StartServer() error {
ctx := context.Background()
StopServer(ctx, server)

return configureServer().ListenAndServe()
return server.ListenAndServe()
}

func StopServer() error {

return server.Shutdown(context.Background())
// Graceful shutdown for HTTP server if Interrupt signal captured from OS
func StopServer(ctx context.Context, srv *http.Server) {

c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt)
go func() {
select {
case <-c:
break
case <-ctx.Done():
break
}

_, cancel := context.WithTimeout(ctx, 5*time.Second)
defer cancel()
_ = srv.Shutdown(ctx)
}()
}
125 changes: 60 additions & 65 deletions services/commands_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,19 @@ package commands

import (
"TDBackend/logger"
"context"
"net/http"
"net/http/httptest"
"testing"
"time"

"github.com/gorilla/mux"
)

func Test_getSessions(t *testing.T) {

logger.Init("debug", time.RFC3339, "TDBackend")

StartServer()
data, _ := http.NewRequest("GET", "http://localhost:8080/api/v1/sessions", nil)
rw := httptest.NewRecorder()
repo["Sessions"] = `test`

type args struct {
w http.ResponseWriter
Expand All @@ -28,8 +28,8 @@ func Test_getSessions(t *testing.T) {
{
name: "getSessions_Success",
args: args{
w: rw,
r: data,
w: httptest.NewRecorder(),
r: httptest.NewRequest(http.MethodGet, "https://localhost/api/v1/sessions", nil),
},
wantPanic: false,
},
Expand All @@ -45,16 +45,12 @@ func Test_getSessions(t *testing.T) {
getSessions(tt.args.w, tt.args.r)
})
}
StopServer()
}

func Test_getDeals(t *testing.T) {

logger.Init("debug", time.RFC3339, "TDBackend")

_ = StartServer()
data, _ := http.NewRequest("GET", "http://localhost:8080/api/v1/deals", nil)
rw := httptest.NewRecorder()
repo["Deals"] = `test`

type args struct {
w http.ResponseWriter
Expand All @@ -68,8 +64,8 @@ func Test_getDeals(t *testing.T) {
{
name: "getDeals_success",
args: args{
w: rw,
r: data,
w: httptest.NewRecorder(),
r: httptest.NewRequest(http.MethodGet, "https://localhost/api/v1/deals", nil),
},
wantPanic: false,
},
Expand All @@ -85,16 +81,12 @@ func Test_getDeals(t *testing.T) {
getDeals(tt.args.w, tt.args.r)
})
}
StopServer()
}

func Test_getPartners(t *testing.T) {

logger.Init("debug", time.RFC3339, "TDBackend")

_ = StartServer()
data, _ := http.NewRequest("GET", "http://localhost:8080/api/v1/partners", nil)
rw := httptest.NewRecorder()
repo["Partners"] = `test`

type args struct {
w http.ResponseWriter
Expand All @@ -108,8 +100,8 @@ func Test_getPartners(t *testing.T) {
{
name: "getPartner_success",
args: args{
w: rw,
r: data,
w: httptest.NewRecorder(),
r: httptest.NewRequest(http.MethodGet, "https://localhost/api/v1/partners", nil),
},
wantPanic: false,
},
Expand All @@ -125,16 +117,12 @@ func Test_getPartners(t *testing.T) {
getPartners(tt.args.w, tt.args.r)
})
}
StopServer()
}

func Test_getKit(t *testing.T) {

logger.Init("debug", time.RFC3339, "TDBackend")

_ = StartServer()
data, _ := http.NewRequest("GET", "http://localhost:8080/api/v1/kit", nil)
rw := httptest.NewRecorder()
repo["Kit"] = `test`

type args struct {
w http.ResponseWriter
Expand All @@ -148,8 +136,8 @@ func Test_getKit(t *testing.T) {
{
name: "getKit_success",
args: args{
w: rw,
r: data,
w: httptest.NewRecorder(),
r: httptest.NewRequest(http.MethodGet, "https://localhost/api/v1/kit", nil),
},
wantPanic: false,
},
Expand All @@ -165,16 +153,12 @@ func Test_getKit(t *testing.T) {
getKit(tt.args.w, tt.args.r)
})
}
StopServer()
}

func Test_getFAQs(t *testing.T) {

logger.Init("debug", time.RFC3339, "TDBackend")

_ = StartServer()
data, _ := http.NewRequest("GET", "http://localhost:8080/api/v1/faqs", nil)
rw := httptest.NewRecorder()
repo["FAQs"] = `test`

type args struct {
w http.ResponseWriter
Expand All @@ -188,8 +172,8 @@ func Test_getFAQs(t *testing.T) {
{
name: "getFAQs_Success",
args: args{
w: rw,
r: data,
w: httptest.NewRecorder(),
r: httptest.NewRequest(http.MethodGet, "https://localhost/api/v1/faqs", nil),
},
wantPanic: false,
},
Expand All @@ -205,18 +189,30 @@ func Test_getFAQs(t *testing.T) {
getFAQs(tt.args.w, tt.args.r)
})
}
StopServer()
}

func TestStartServer(t *testing.T) {
func Test_serveImage(t *testing.T) {

logger.Init("debug", time.RFC3339, "TDBackend")

r := httptest.NewRequest(http.MethodGet, "http://localhost:8080/api/v1/img/FFMC.jpg", nil)
r = mux.SetURLVars(r, map[string]string{"name": "FFMC.jpg"})

type args struct {
w http.ResponseWriter
r *http.Request
}
tests := []struct {
name string
args args
wantPanic bool
}{
{
name: "StartServer_Success",
name: "getFFMC_Success",
args: args{
w: httptest.NewRecorder(),
r: r,
},
wantPanic: false,
},
}
Expand All @@ -225,51 +221,50 @@ func TestStartServer(t *testing.T) {
defer func() {
r := recover()
if (r != nil) != tt.wantPanic {
t.Errorf("getSessions(), wantPanic = %v", tt.wantPanic)
t.Errorf("serveImage(), wantPanic = %v", tt.wantPanic)
}
}()
_ = StartServer()
serveImage(tt.args.w, tt.args.r)
})
}
StopServer()
}

func Test_serveImage(t *testing.T) {

logger.Init("debug", time.RFC3339, "TDBackend")

_ = StartServer()
data, _ := http.NewRequest("GET", "http://localhost:8080/api/v1/img/FFMC.jpg", nil)
rw := httptest.NewRecorder()

func TestStopServer(t *testing.T) {
type args struct {
w http.ResponseWriter
r *http.Request
ctx context.Context
srv *http.Server
}
tests := []struct {
name string
args args
wantPanic bool
name string
args args
}{
{
name: "getFFMC_Success",
name: "StopServer_Success",
args: args{
w: rw,
r: data,
ctx: context.Background(),
srv: &http.Server{},
},
wantPanic: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
defer func() {
r := recover()
if (r != nil) != tt.wantPanic {
t.Errorf("serveImage(), wantPanic = %v", tt.wantPanic)
}
}()
serveImage(tt.args.w, tt.args.r)
StopServer(tt.args.ctx, tt.args.srv)
})
}
}

func TestStartServer(t *testing.T) {
tests := []struct {
name string
wantErr bool
}{
// TODO: Add test cases.
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if err := StartServer(); (err != nil) != tt.wantErr {
t.Errorf("StartServer() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
StopServer()
}
9 changes: 7 additions & 2 deletions services/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func readFile(endPoint string) (string, error) {
t := time.Now()

// Get file name
fileName := "./static/" + endPoint + ".json"
fileName := filepath.Join("static", fmt.Sprintf("%s.json", endPoint))

// Open our jsonFile
jsonFile, err := os.Open(filepath.Clean(fileName))
Expand Down Expand Up @@ -100,7 +100,12 @@ func getImage(w http.ResponseWriter, r *http.Request) (int, error) {

// Get file name
params := mux.Vars(r)
fileName := "static/img/" + params["name"]

fmt.Println(params)

fmt.Println("image name: ", params["name"])
// Get file name
fileName := filepath.Join("static/img", params["name"])

// Open our jsonFile
jsonFile, err := os.Open(filepath.Clean(fileName))
Expand Down
Loading

0 comments on commit 7098e08

Please sign in to comment.