Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
133 changes: 133 additions & 0 deletions ecserver/AddCandidate_8f9ed01d08_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
package main

import (
"bytes"
"encoding/json"
"net/http"
"net/http/httptest"
"testing"
)

type Candidate struct {
Name string `json:"name"`
ImageUrl string `json:"imageUrl"`
}

type BasicResponse struct {
Status int `json:"status"`
Message string `json:"message"`
}

var candidates = []*Candidate{
&Candidate{
Name: "John Doe",
ImageUrl: "http://example.com/johndoe.jpg",
},
}

func addCandidate(w http.ResponseWriter, r *http.Request) {
newCandidate := &Candidate{}
err := json.NewDecoder(r.Body).Decode(newCandidate)
if err != nil {
resp := &BasicResponse{
Status: http.StatusBadRequest,
Message: "Invalid request payload",
}
writeBasicResponse(w, resp)
return
}

isCandidatePresent := false
for i, ca := range candidates {
if newCandidate.Name == ca.Name {
isCandidatePresent = true
candidates[i].Name = newCandidate.Name
candidates[i].ImageUrl = newCandidate.ImageUrl
}
}

if !isCandidatePresent {
candidates = append(candidates, newCandidate)
}

writeAllCandidatesResponse(w)
}

func writeBasicResponse(w http.ResponseWriter, resp *BasicResponse) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(resp.Status)
json.NewEncoder(w).Encode(resp)
}

func writeAllCandidatesResponse(w http.ResponseWriter) {
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(candidates)
}

func TestAddCandidate_8f9ed01d08(t *testing.T) {
t.Run("test add new candidate", func(t *testing.T) {
data := []byte(`{"name":"Jane Doe","imageUrl":"http://example.com/janedoe.jpg"}`)
req, err := http.NewRequest("POST", "/addCandidate", bytes.NewBuffer(data))
if err != nil {
t.Fatal(err)
}

rr := httptest.NewRecorder()
handler := http.HandlerFunc(addCandidate)

handler.ServeHTTP(rr, req)

if status := rr.Code; status != http.StatusOK {
t.Errorf("handler returned wrong status code: got %v want %v", status, http.StatusOK)
}

expected := `{"name":"Jane Doe","imageUrl":"http://example.com/janedoe.jpg"}`
if rr.Body.String() != expected {
t.Errorf("handler returned unexpected body: got %v want %v", rr.Body.String(), expected)
}
})

t.Run("test add existing candidate", func(t *testing.T) {
data := []byte(`{"name":"John Doe","imageUrl":"http://example.com/johndoe.jpg"}`)
req, err := http.NewRequest("POST", "/addCandidate", bytes.NewBuffer(data))
if err != nil {
t.Fatal(err)
}

rr := httptest.NewRecorder()
handler := http.HandlerFunc(addCandidate)

handler.ServeHTTP(rr, req)

if status := rr.Code; status != http.StatusOK {
t.Errorf("handler returned wrong status code: got %v want %v", status, http.StatusOK)
}

expected := `{"name":"John Doe","imageUrl":"http://example.com/johndoe.jpg"}`
if rr.Body.String() != expected {
t.Errorf("handler returned unexpected body: got %v want %v", rr.Body.String(), expected)
}
})

t.Run("test add candidate with invalid payload", func(t *testing.T) {
data := []byte(`{"name":"John Doe","url":"http://example.com/johndoe.jpg"}`)
req, err := http.NewRequest("POST", "/addCandidate", bytes.NewBuffer(data))
if err != nil {
t.Fatal(err)
}

rr := httptest.NewRecorder()
handler := http.HandlerFunc(addCandidate)

handler.ServeHTTP(rr, req)

if status := rr.Code; status != http.StatusBadRequest {
t.Errorf("handler returned wrong status code: got %v want %v", status, http.StatusBadRequest)
}

expected := `{"status":400,"message":"Invalid request payload"}`
if rr.Body.String() != expected {
t.Errorf("handler returned unexpected body: got %v want %v", rr.Body.String(), expected)
}
})
}
49 changes: 49 additions & 0 deletions ecserver/DeleteCandidate_9be834270b_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// Test generated by RoostGPT for test test using AI Type Open AI and AI Model gpt-4

package main

import (
"bytes"
"encoding/json"
"net/http"
"net/http/httptest"
"testing"
)

func TestDeleteCandidate_9be834270b(t *testing.T) {
// Test case 1: Delete an existing candidate
req, err := http.NewRequest("DELETE", "/candidate", bytes.NewBuffer([]byte(`{"name": "John Doe"}`)))
if err != nil {
t.Fatal(err)
}
rr := httptest.NewRecorder()
handler := http.HandlerFunc(deleteCandidate)
handler.ServeHTTP(rr, req)
if status := rr.Code; status != http.StatusOK {
t.Errorf("handler returned wrong status code: got %v want %v", status, http.StatusOK)
}

// Test case 2: Delete a non-existing candidate
req, err = http.NewRequest("DELETE", "/candidate", bytes.NewBuffer([]byte(`{"name": "Non-existing Candidate"}`)))
if err != nil {
t.Fatal(err)
}
rr = httptest.NewRecorder()
handler = http.HandlerFunc(deleteCandidate)
handler.ServeHTTP(rr, req)
if status := rr.Code; status != http.StatusBadRequest {
t.Errorf("handler returned wrong status code: got %v want %v", status, http.StatusBadRequest)
}

// Test case 3: Send invalid payload
req, err = http.NewRequest("DELETE", "/candidate", bytes.NewBuffer([]byte(`{"invalid": "payload"}`)))
if err != nil {
t.Fatal(err)
}
rr = httptest.NewRecorder()
handler = http.HandlerFunc(deleteCandidate)
handler.ServeHTTP(rr, req)
if status := rr.Code; status != http.StatusBadRequest {
t.Errorf("handler returned wrong status code: got %v want %v", status, http.StatusBadRequest)
}
}
53 changes: 53 additions & 0 deletions ecserver/GetAllCandidates_414119e2ca_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// Test generated by RoostGPT for test test using AI Type Open AI and AI Model gpt-4

package main

import (
"net/http"
"net/http/httptest"
"testing"
)

func TestGetAllCandidates_414119e2ca(t *testing.T) {
t.Run("returns All Candidates", func(t *testing.T) {
request, _ := http.NewRequest(http.MethodGet, "/candidates", nil)
response := httptest.NewRecorder()

getAllCandidates(response, request)

got := response.Body.String()
want := "All Candidates"

if got != want {
t.Errorf("got %q, want %q", got, want)
}
})

t.Run("returns status code 200", func(t *testing.T) {
request, _ := http.NewRequest(http.MethodGet, "/candidates", nil)
response := httptest.NewRecorder()

getAllCandidates(response, request)

got := response.Result().StatusCode
want := http.StatusOK

if got != want {
t.Errorf("got %d, want %d", got, want)
}
})

t.Run("returns status code 404 when route is not found", func(t *testing.T) {
request, _ := http.NewRequest(http.MethodGet, "/not-a-route", nil)
response := httptest.NewRecorder()

getAllCandidates(response, request)

got := response.Result().StatusCode
want := http.StatusNotFound

if got != want {
t.Errorf("got %d, want %d", got, want)
}
})
}
49 changes: 49 additions & 0 deletions ecserver/HandleInvalidMethod_58fd8542d1_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package main

import (
"encoding/json"
"net/http"
"net/http/httptest"
"testing"
)

type BasicResponse struct {
Status int
Message string
}

func handleInvalidMethod(w http.ResponseWriter, r *http.Request) {
resp := &BasicResponse{
Status: http.StatusMethodNotAllowed,
Message: "No such endpoint",
}
writeBasicResponse(w, resp)
}

func writeBasicResponse(w http.ResponseWriter, resp *BasicResponse) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(resp.Status)
json.NewEncoder(w).Encode(resp)
}

func TestHandleInvalidMethod_58fd8542d1(t *testing.T) {
// Test case 1: Check if the status code returned is 405 (Method Not Allowed)
req, err := http.NewRequest("GET", "", nil)
if err != nil {
t.Fatal(err)
}
rr := httptest.NewRecorder()
handler := http.HandlerFunc(handleInvalidMethod)
handler.ServeHTTP(rr, req)
if status := rr.Code; status != http.StatusMethodNotAllowed {
t.Errorf("handler returned wrong status code: got %v want %v",
status, http.StatusMethodNotAllowed)
}

// Test case 2: Check if the message returned is "No such endpoint"
expected := `{"Status":405,"Message":"No such endpoint"}`
if rr.Body.String() != expected {
t.Errorf("handler returned unexpected body: got %v want %v",
rr.Body.String(), expected)
}
}
72 changes: 72 additions & 0 deletions ecserver/ServeRoot_da512bfd0e_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
// Test generated by RoostGPT for test test using AI Type Open AI and AI Model gpt-4

package main

import (
"net/http"
"net/http/httptest"
"strings"
"testing"
)

func TestServeRoot_da512bfd0e(t *testing.T) {
req, err := http.NewRequest("GET", "/", nil)
if err != nil {
t.Fatal(err)
}

rr := httptest.NewRecorder()
handler := http.HandlerFunc(serveRoot)

handler.ServeHTTP(rr, req)

if status := rr.Code; status != http.StatusOK {
t.Errorf("handler returned wrong status code: got %v want %v",
status, http.StatusOK)
}

req, err = http.NewRequest("DELETE", "/", nil)
if err != nil {
t.Fatal(err)
}

rr = httptest.NewRecorder()
handler = http.HandlerFunc(serveRoot)

handler.ServeHTTP(rr, req)

if status := rr.Code; status != http.StatusOK {
t.Errorf("handler returned wrong status code: got %v want %v",
status, http.StatusOK)
}

req, err = http.NewRequest("POST", "/", strings.NewReader("test"))
if err != nil {
t.Fatal(err)
}

rr = httptest.NewRecorder()
handler = http.HandlerFunc(serveRoot)

handler.ServeHTTP(rr, req)

if status := rr.Code; status != http.StatusOK {
t.Errorf("handler returned wrong status code: got %v want %v",
status, http.StatusOK)
}

req, err = http.NewRequest("PUT", "/", nil)
if err != nil {
t.Fatal(err)
}

rr = httptest.NewRecorder()
handler = http.HandlerFunc(serveRoot)

handler.ServeHTTP(rr, req)

if status := rr.Code; status != http.StatusOK {
t.Errorf("handler returned wrong status code: got %v want %v",
status, http.StatusOK)
}
}
Loading