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

Commit

Permalink
Merge pull request #46 from rehabaam/feat/add-coverage
Browse files Browse the repository at this point in the history
Adding more test coverage
  • Loading branch information
rehabaam authored Nov 11, 2023
2 parents c3a4866 + ebb78ba commit 591163f
Show file tree
Hide file tree
Showing 6 changed files with 182 additions and 57 deletions.
6 changes: 3 additions & 3 deletions TDWorker.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@ func main() {

// startServer func starts an HTTP server that exposes RESTful APIs
func startServer() error {
logger.AppLogger(labels.Info,labels.ServerStarting,0)
err := commands.RunServer()
logger.AppLogger(labels.Info, labels.ServerStarting, 0)
err := commands.StartServer()
if err != nil {
// Print the error occured while starting the server.
fmt.Printf("%+v\n", err.Error())
}
return err
}
}
21 changes: 0 additions & 21 deletions middleware/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,8 @@ package middleware

import (
"context"
"crypto/rand"
"encoding/base64"
"fmt"
"net/http"
"os"
"strings"
"sync/atomic"
)

Expand All @@ -25,23 +21,6 @@ var (
reqID uint64
)

// init Initializes constant part of request ID
func init() {
hostname, err := os.Hostname()
if hostname == "" || err != nil {
hostname = "localhost"
}
var buf [12]byte
var b64 string
for len(b64) < 10 {
_, _ = rand.Read(buf[:])
b64 = base64.StdEncoding.EncodeToString(buf[:])
b64 = strings.NewReplacer("+", "", "/", "").Replace(b64)
}

prefix = fmt.Sprintf("%s/%s", hostname, b64[0:10])
}

// RequestID is a middleware that injects a request ID into the context of each
// request. A request ID is a string of the form "host.example.com/random-0001",
// where "random" is a base62 random string that uniquely identifies this go
Expand Down
21 changes: 18 additions & 3 deletions services/commands.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
package commands

import (
"context"
"net/http"
"time"

"github.com/gorilla/mux"
)

var (
server *http.Server
)

// getPartners func for getting TriDubai annually partners
func getPartners(w http.ResponseWriter, r *http.Request) {
_, _ = getData("Partners", w, r)
Expand Down Expand Up @@ -38,7 +43,7 @@ func serveImage(w http.ResponseWriter, r *http.Request) {
}

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

loadFileToMemory()
r := mux.NewRouter()
Expand All @@ -51,10 +56,20 @@ func RunServer() error {
api.HandleFunc("/faqs", getFAQs).Methods(http.MethodGet)
api.HandleFunc("/img/{name}", serveImage).Methods(http.MethodGet)

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

func StartServer() error {

return configureServer().ListenAndServe()
}

func StopServer() error {

return server.Shutdown(context.Background())
}
111 changes: 85 additions & 26 deletions services/commands_test.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,23 @@
package commands

import (
"TDBackend/logger"
"fmt"
"net/http"
"net/http/httptest"
"testing"
"time"
)

func Test_getSessions(t *testing.T) {

data, _ := http.NewRequest("GET", "https://apps.tridubai.org/api/v1/sessions", nil)
logger.Init("debug", time.RFC3339, "TDBackend")

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

fmt.Println("test")
type args struct {
w http.ResponseWriter
r *http.Request
Expand All @@ -21,12 +28,12 @@ func Test_getSessions(t *testing.T) {
wantPanic bool
}{
{
name: "getSessions_failure",
name: "getSessions_Success",
args: args{
w: rw,
r: data,
},
wantPanic: true,
wantPanic: false,
},
}
for _, tt := range tests {
Expand All @@ -40,11 +47,15 @@ func Test_getSessions(t *testing.T) {
getSessions(tt.args.w, tt.args.r)
})
}
StopServer()
}

func Test_getDeals(t *testing.T) {

data, _ := http.NewRequest("GET", "https://apps.tridubai.org/api/v1/deals", nil)
logger.Init("debug", time.RFC3339, "TDBackend")

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

type args struct {
Expand All @@ -56,21 +67,13 @@ func Test_getDeals(t *testing.T) {
args args
wantPanic bool
}{
{
name: "getDeals_failure",
args: args{
w: rw,
r: data,
},
wantPanic: true,
},
{
name: "getDeals_success",
args: args{
w: rw,
r: data,
},
wantPanic: true,
wantPanic: false,
},
}
for _, tt := range tests {
Expand All @@ -84,11 +87,15 @@ func Test_getDeals(t *testing.T) {
getDeals(tt.args.w, tt.args.r)
})
}
StopServer()
}

func Test_getPartners(t *testing.T) {

data, _ := http.NewRequest("GET", "https://apps.tridubai.org/api/v1/partners", nil)
logger.Init("debug", time.RFC3339, "TDBackend")

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

type args struct {
Expand All @@ -101,12 +108,12 @@ func Test_getPartners(t *testing.T) {
wantPanic bool
}{
{
name: "getPartner_failure",
name: "getPartner_success",
args: args{
w: rw,
r: data,
},
wantPanic: true,
wantPanic: false,
},
}
for _, tt := range tests {
Expand All @@ -120,11 +127,15 @@ func Test_getPartners(t *testing.T) {
getPartners(tt.args.w, tt.args.r)
})
}
StopServer()
}

func Test_getKit(t *testing.T) {

data, _ := http.NewRequest("GET", "https://apps.tridubai.org/api/v1/kit", nil)
logger.Init("debug", time.RFC3339, "TDBackend")

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

type args struct {
Expand All @@ -137,12 +148,12 @@ func Test_getKit(t *testing.T) {
wantPanic bool
}{
{
name: "getKit_failure",
name: "getKit_success",
args: args{
w: rw,
r: data,
},
wantPanic: true,
wantPanic: false,
},
}
for _, tt := range tests {
Expand All @@ -156,11 +167,15 @@ func Test_getKit(t *testing.T) {
getKit(tt.args.w, tt.args.r)
})
}
StopServer()
}

func Test_getFAQs(t *testing.T) {

data, _ := http.NewRequest("GET", "https://apps.tridubai.org/api/v1/faqs", nil)
logger.Init("debug", time.RFC3339, "TDBackend")

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

type args struct {
Expand All @@ -173,12 +188,12 @@ func Test_getFAQs(t *testing.T) {
wantPanic bool
}{
{
name: "getFAQs_failure",
name: "getFAQs_Success",
args: args{
w: rw,
r: data,
},
wantPanic: true,
wantPanic: false,
},
}
for _, tt := range tests {
Expand All @@ -192,16 +207,19 @@ func Test_getFAQs(t *testing.T) {
getFAQs(tt.args.w, tt.args.r)
})
}
StopServer()
}

func TestRunServer(t *testing.T) {
func TestStartServer(t *testing.T) {
logger.Init("debug", time.RFC3339, "TDBackend")

tests := []struct {
name string
wantPanic bool
}{
{
name: "getSessions_failure",
wantPanic: true,
name: "StartServer_Success",
wantPanic: false,
},
}
for _, tt := range tests {
Expand All @@ -212,7 +230,48 @@ func TestRunServer(t *testing.T) {
t.Errorf("getSessions(), wantPanic = %v", tt.wantPanic)
}
}()
RunServer()
_ = StartServer()
})
}
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()

type args struct {
w http.ResponseWriter
r *http.Request
}
tests := []struct {
name string
args args
wantPanic bool
}{
{
name: "getFFMC_Success",
args: args{
w: rw,
r: data,
},
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()
}
3 changes: 2 additions & 1 deletion services/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,12 @@ func readFile(endPoint string) (string, error) {
t := time.Now()

// Get file name
fileName := "static/" + endPoint + ".json"
fileName := "./static/" + endPoint + ".json"

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

fmt.Println("File", jsonFile, err)
// if we os.Open returns an error then handle it
if err != nil {
logger.AppLogger(labels.Error, "Error while opening "+endPoint+" file!", time.Since(t).Nanoseconds(), labels.Error+"|"+err.Error())
Expand Down
Loading

0 comments on commit 591163f

Please sign in to comment.