Fix testing issues & add test data

This commit is contained in:
NGnius 2020-03-06 14:32:33 -05:00
parent 5dede630e4
commit 77db5a33ec
2 changed files with 29 additions and 37 deletions

View file

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

60
main.go
View file

@ -12,15 +12,13 @@ import (
"os" "os"
"os/signal" "os/signal"
"path/filepath" "path/filepath"
"strconv"
) )
const ( const (
defaultPassword = "" defaultPassword = ""
defaultEntryURL = "http://localhost:1337/record" defaultEntryURL = "http://localhost:1337/record"
defaultPort = "9000" defaultPort = "9000"
defaultDir = "criterias" defaultDir = "criterias"
defaultCriteriaJson = "criteria.json"
) )
var ( var (
@ -30,7 +28,6 @@ var (
port string port string
dir string dir string
// internal // internal
handler http.Handler
server *http.Server server *http.Server
client *http.Client client *http.Client
isClosing bool isClosing bool
@ -105,7 +102,7 @@ func main() {
}() }()
server = &http.Server{ server = &http.Server{
Addr: ":" + port, Addr: ":" + port,
Handler: handler, Handler: serverMux,
} }
client = &http.Client{} client = &http.Client{}
fmt.Println("Starting on " + server.Addr) fmt.Println("Starting on " + server.Addr)
@ -118,7 +115,6 @@ func main() {
// criteria POST request handler // criteria POST request handler
// this also sends a new entry to leadercraft-s when the criteria is met // this also sends a new entry to leadercraft-s when the criteria is met
func criteriaHandler(w http.ResponseWriter, r *http.Request) { func criteriaHandler(w http.ResponseWriter, r *http.Request) {
// TODO
if r.Method != "POST" { if r.Method != "POST" {
w.WriteHeader(405) w.WriteHeader(405)
return return
@ -139,52 +135,38 @@ func criteriaHandler(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(400) w.WriteHeader(400)
return return
} }
// TODO load criteria for game id if reqCriteria.GameID < 2 {
criteriaFilename := filepath.Join(dir, fmt.Sprintf("%d", reqCriteria.GameID)+".json") //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{} realCriteria := &Criteria{}
realCriteriaF, openErr := os.Open(criteriaFilename) realCriteriaF, openErr := os.Open(criteriaFilename)
if openErr != nil { if openErr != nil {
// file not found (or not accessible)
//fmt.Println("404 -- criteria file not accessible: %s", openErr.Error())
w.WriteHeader(404) w.WriteHeader(404)
return return
} }
realCritData, realCritReadErr := ioutil.ReadAll(realCriteriaF) realCritData, realCritReadErr := ioutil.ReadAll(realCriteriaF)
if realCritReadErr != nil { if realCritReadErr != nil {
w.WriteHeader(404) // internal read error
w.WriteHeader(500)
return return
} }
unmarshCritErr := json.Unmarshal(realCritData, realCriteria) unmarshCritErr := json.Unmarshal(realCritData, realCriteria)
if unmarshCritErr != nil { if unmarshCritErr != nil {
// criteria file is invalid json // criteria file is invalid json
w.WriteHeader(404) //fmt.Printf("404 -- Invalid criteria file json: %s", unmarshCritErr.Error())
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
w.WriteHeader(404) w.WriteHeader(404)
return return
} }
// TODO check if criteria matches // TODO check if criteria matches
if !realCriteria.Meets(reqCriteria) { if !reqCriteria.Meets(realCriteria) {
// if criteria does not match, stop // if criteria does not match, stop
//fmt.Println("400 -- Criteria does not meet required criteria")
w.WriteHeader(400) w.WriteHeader(400)
return return
} }
@ -197,6 +179,7 @@ func criteriaHandler(w http.ResponseWriter, r *http.Request) {
} }
echoData, marshErr := json.Marshal(entry) echoData, marshErr := json.Marshal(entry)
if marshErr != nil { if marshErr != nil {
//fmt.Println("500 -- Unable to marshal entry into JSON for leaderboard-s endpoint")
w.WriteHeader(500) w.WriteHeader(500)
return return
} }
@ -204,6 +187,7 @@ func criteriaHandler(w http.ResponseWriter, r *http.Request) {
entryReq, reqErr := http.NewRequest("POST", entryURL, echoBody) entryReq, reqErr := http.NewRequest("POST", entryURL, echoBody)
if reqErr != nil { if reqErr != nil {
// malformed request parameters // malformed request parameters
//fmt.Println("500 -- Malformed request detected during initialization")
w.WriteHeader(500) w.WriteHeader(500)
return return
} }
@ -212,6 +196,7 @@ func criteriaHandler(w http.ResponseWriter, r *http.Request) {
echoResp, postErr := client.Do(entryReq) echoResp, postErr := client.Do(entryReq)
if postErr != nil { if postErr != nil {
// bad communication or malformed request // bad communication or malformed request
//fmt.Println("500 -- Bad communication for leadercraft-s")
w.WriteHeader(500) w.WriteHeader(500)
return return
} }
@ -220,6 +205,7 @@ func criteriaHandler(w http.ResponseWriter, r *http.Request) {
echoRespData, echoReadErr := ioutil.ReadAll(echoResp.Body) echoRespData, echoReadErr := ioutil.ReadAll(echoResp.Body)
if echoReadErr != nil { if echoReadErr != nil {
// body read error (should never occur) // body read error (should never occur)
//fmt.Println("!!! Error reading response body from leadercraft-s")
return return
} }
w.Write(echoRespData) w.Write(echoRespData)