Browse Source

Fix testing issues & add test data

master
NGnius 4 years ago
parent
commit
77db5a33ec
2 changed files with 29 additions and 37 deletions
  1. +6
    -0
      criterias/criteria-2.json
  2. +23
    -37
      main.go

+ 6
- 0
criterias/criteria-2.json View File

@@ -0,0 +1,6 @@
{
"Location":[[-0.1,-0.1,-0.1],[2,2,2]],
"GameID": 2,
"Coefficient": 42,
"ScoreMode": "time"
}

+ 23
- 37
main.go View File

@@ -12,15 +12,13 @@ import (
"os"
"os/signal"
"path/filepath"
"strconv"
)

const (
defaultPassword = ""
defaultEntryURL = "http://localhost:1337/record"
defaultPort = "9000"
defaultDir = "criterias"
defaultCriteriaJson = "criteria.json"
defaultPassword = ""
defaultEntryURL = "http://localhost:1337/record"
defaultPort = "9000"
defaultDir = "criterias"
)

var (
@@ -30,7 +28,6 @@ var (
port string
dir string
// internal
handler http.Handler
server *http.Server
client *http.Client
isClosing bool
@@ -105,7 +102,7 @@ func main() {
}()
server = &http.Server{
Addr: ":" + port,
Handler: handler,
Handler: serverMux,
}
client = &http.Client{}
fmt.Println("Starting on " + server.Addr)
@@ -118,7 +115,6 @@ func main() {
// criteria POST request handler
// this also sends a new entry to leadercraft-s when the criteria is met
func criteriaHandler(w http.ResponseWriter, r *http.Request) {
// TODO
if r.Method != "POST" {
w.WriteHeader(405)
return
@@ -139,52 +135,38 @@ func criteriaHandler(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(400)
return
}
// TODO load criteria for game id
criteriaFilename := filepath.Join(dir, fmt.Sprintf("%d", reqCriteria.GameID)+".json")
if reqCriteria.GameID < 2 {
//fmt.Println("404 -- GameID too low")
w.WriteHeader(404)
return
}
criteriaFilename := filepath.Join(dir, fmt.Sprintf("criteria-%d.json", reqCriteria.GameID))
//fmt.Println(criteriaFilename)
realCriteria := &Criteria{}
realCriteriaF, openErr := os.Open(criteriaFilename)
if openErr != nil {
// file not found (or not accessible)
//fmt.Println("404 -- criteria file not accessible: %s", openErr.Error())
w.WriteHeader(404)
return
}
realCritData, realCritReadErr := ioutil.ReadAll(realCriteriaF)
if realCritReadErr != nil {
w.WriteHeader(404)
// internal read error
w.WriteHeader(500)
return
}
unmarshCritErr := json.Unmarshal(realCritData, realCriteria)
if unmarshCritErr != nil {
// criteria file is invalid json
w.WriteHeader(404)
return
}
if reqCriteria.GameID > 1 {
f, fileErr := os.Open(filepath.Join(dir, "criteria-"+strconv.Itoa(int(reqCriteria.GameID))+".json"))
if fileErr != nil {
// file not found
w.WriteHeader(404)
return
}
data, readErr = ioutil.ReadAll(f)
if readErr != nil {
// file could not be read properly (file doesn't exist?)
w.WriteHeader(404)
return
}
unmarshErr = json.Unmarshal(data, &realCriteria)
if unmarshErr != nil {
// data could not be interpreted as json
w.WriteHeader(500)
return
}
} else {
// Game ID cannot exist
//fmt.Printf("404 -- Invalid criteria file json: %s", unmarshCritErr.Error())
w.WriteHeader(404)
return
}
// TODO check if criteria matches
if !realCriteria.Meets(reqCriteria) {
if !reqCriteria.Meets(realCriteria) {
// if criteria does not match, stop
//fmt.Println("400 -- Criteria does not meet required criteria")
w.WriteHeader(400)
return
}
@@ -197,6 +179,7 @@ func criteriaHandler(w http.ResponseWriter, r *http.Request) {
}
echoData, marshErr := json.Marshal(entry)
if marshErr != nil {
//fmt.Println("500 -- Unable to marshal entry into JSON for leaderboard-s endpoint")
w.WriteHeader(500)
return
}
@@ -204,6 +187,7 @@ func criteriaHandler(w http.ResponseWriter, r *http.Request) {
entryReq, reqErr := http.NewRequest("POST", entryURL, echoBody)
if reqErr != nil {
// malformed request parameters
//fmt.Println("500 -- Malformed request detected during initialization")
w.WriteHeader(500)
return
}
@@ -212,6 +196,7 @@ func criteriaHandler(w http.ResponseWriter, r *http.Request) {
echoResp, postErr := client.Do(entryReq)
if postErr != nil {
// bad communication or malformed request
//fmt.Println("500 -- Bad communication for leadercraft-s")
w.WriteHeader(500)
return
}
@@ -220,6 +205,7 @@ func criteriaHandler(w http.ResponseWriter, r *http.Request) {
echoRespData, echoReadErr := ioutil.ReadAll(echoResp.Body)
if echoReadErr != nil {
// body read error (should never occur)
//fmt.Println("!!! Error reading response body from leadercraft-s")
return
}
w.Write(echoRespData)