From 2a27d6e6703b1c2f477df96b1cf13f757988b853 Mon Sep 17 00:00:00 2001 From: tanbirali Date: Wed, 2 Oct 2024 12:23:06 +0530 Subject: [PATCH 1/3] feat: Resolved the merge conflict arising --- internal/server/httpServer.go | 56 +++++++++++++++++++++++++++++++++-- 1 file changed, 54 insertions(+), 2 deletions(-) diff --git a/internal/server/httpServer.go b/internal/server/httpServer.go index 68e4c86..7b21f8a 100644 --- a/internal/server/httpServer.go +++ b/internal/server/httpServer.go @@ -5,8 +5,10 @@ import ( "encoding/json" "errors" "fmt" + "io" "log" "net/http" + "os" "server/internal/middleware" "strings" "sync" @@ -128,6 +130,56 @@ func (s *HTTPServer) CliHandler(w http.ResponseWriter, r *http.Request) { } } -func (s *HTTPServer) SearchHandler(w http.ResponseWriter, request *http.Request) { - util.JSONResponse(w, http.StatusOK, map[string]string{"message": "Search results"}) +func (s *HTTPServer) SearchHandler(w http.ResponseWriter, request *http.Request) {q := request.URL.Query().Get("q") +if q == "" { + http.Error(w, "Missing query parameter 'q' ", http.StatusBadRequest) + return +} +if q == "*" { + q = "" +} +f, err := os.Open("https://github.com/DiceDB/playground-web/blob/master/src/data/command.ts") +if err != nil { + log.Fatal(err) +} +defer f.Close() +data, err := io.ReadAll(f) +if err != nil { + log.Fatal(err) +} +var commands map[string]map[string]string +err = json.Unmarshal(data, &commands) +if err != nil { + log.Fatal(err) +} +matchingCommands := []map[string]string{} +for _, command := range commands { + + title, okTitle := command["title"] + body, okBody:= command["body"] + if okTitle && okBody { + if strings.Contains(strings.ToLower(title), q) || + strings.Contains(strings.ToLower(body), q) { + + highlightedText := strings.ReplaceAll(title, q, ""+q+"") + + matchingCommands = append(matchingCommands, map[string]string{ + "title": highlightedText, + "syntax": command["syntax"], + "body": body, + "url": command["url"], + } ) + } + } +} +if len(matchingCommands) == 0 { + util.JSONResponse(w, http.StatusOK, map[string]string{"message": "No search results"}) + return +} +response := map[string]interface{}{ + "total": len(matchingCommands), + "results": matchingCommands, +} + +util.JSONResponse(w, http.StatusOK, response) } From e14ab312bf81ef7b98377492db59c1fd898a7f07 Mon Sep 17 00:00:00 2001 From: tanbirali Date: Wed, 2 Oct 2024 19:13:51 +0530 Subject: [PATCH 2/3] fix: linting issues fixed --- internal/server/httpServer.go | 99 ++++++++++++++++++----------------- 1 file changed, 50 insertions(+), 49 deletions(-) diff --git a/internal/server/httpServer.go b/internal/server/httpServer.go index 7b21f8a..9721ee1 100644 --- a/internal/server/httpServer.go +++ b/internal/server/httpServer.go @@ -130,56 +130,57 @@ func (s *HTTPServer) CliHandler(w http.ResponseWriter, r *http.Request) { } } -func (s *HTTPServer) SearchHandler(w http.ResponseWriter, request *http.Request) {q := request.URL.Query().Get("q") -if q == "" { - http.Error(w, "Missing query parameter 'q' ", http.StatusBadRequest) - return -} -if q == "*" { - q = "" -} -f, err := os.Open("https://github.com/DiceDB/playground-web/blob/master/src/data/command.ts") -if err != nil { - log.Fatal(err) -} -defer f.Close() -data, err := io.ReadAll(f) -if err != nil { - log.Fatal(err) -} -var commands map[string]map[string]string -err = json.Unmarshal(data, &commands) -if err != nil { - log.Fatal(err) -} -matchingCommands := []map[string]string{} -for _, command := range commands { - - title, okTitle := command["title"] - body, okBody:= command["body"] - if okTitle && okBody { - if strings.Contains(strings.ToLower(title), q) || - strings.Contains(strings.ToLower(body), q) { - - highlightedText := strings.ReplaceAll(title, q, ""+q+"") - - matchingCommands = append(matchingCommands, map[string]string{ - "title": highlightedText, - "syntax": command["syntax"], - "body": body, - "url": command["url"], - } ) +func (s *HTTPServer) SearchHandler(w http.ResponseWriter, request *http.Request) { + q := request.URL.Query().Get("q") + if q == "" { + http.Error(w, "Missing query parameter 'q' ", http.StatusBadRequest) + return + } + if q == "*" { + q = "" + } + f, err := os.Open("https://github.com/DiceDB/playground-web/blob/master/src/data/command.ts") + if err != nil { + log.Fatal(err) + } + defer f.Close() + data, err := io.ReadAll(f) + if err != nil { + log.Fatal(err) + } + var commands map[string]map[string]string + err = json.Unmarshal(data, &commands) + if err != nil { + log.Fatal(err) + } + matchingCommands := []map[string]string{} + for _, command := range commands { + + title, okTitle := command["title"] + body, okBody := command["body"] + if okTitle && okBody { + if strings.Contains(strings.ToLower(title), q) || + strings.Contains(strings.ToLower(body), q) { + + highlightedText := strings.ReplaceAll(title, q, ""+q+"") + + matchingCommands = append(matchingCommands, map[string]string{ + "title": highlightedText, + "syntax": command["syntax"], + "body": body, + "url": command["url"], + }) + } } } -} -if len(matchingCommands) == 0 { - util.JSONResponse(w, http.StatusOK, map[string]string{"message": "No search results"}) - return -} -response := map[string]interface{}{ - "total": len(matchingCommands), - "results": matchingCommands, -} + if len(matchingCommands) == 0 { + util.JSONResponse(w, http.StatusOK, map[string]string{"message": "No search results"}) + return + } + response := map[string]interface{}{ + "total": len(matchingCommands), + "results": matchingCommands, + } -util.JSONResponse(w, http.StatusOK, response) + util.JSONResponse(w, http.StatusOK, response) } From 102bbe91f24110d6aebaea6a89dbe107ffcc7268 Mon Sep 17 00:00:00 2001 From: tanbirali Date: Mon, 7 Oct 2024 23:44:00 +0530 Subject: [PATCH 3/3] added unit test for the search Handler --- .../integration/apiSearchHandler_test.go | 71 +++++++++++++++++++ 1 file changed, 71 insertions(+) create mode 100644 internal/tests/integration/apiSearchHandler_test.go diff --git a/internal/tests/integration/apiSearchHandler_test.go b/internal/tests/integration/apiSearchHandler_test.go new file mode 100644 index 0000000..6069c90 --- /dev/null +++ b/internal/tests/integration/apiSearchHandler_test.go @@ -0,0 +1,71 @@ +package main + +import ( + "io/ioutil" + "net/http" + "net/http/httptest" + "strings" + "testing" +) + +func TestSearchHandler(t *testing.T) { + tests := []struct { + name string + query string + expectedStatus int + expectedBody string + }{ + { + name: "Missing query parameter", + query: "", + expectedStatus: http.StatusBadRequest, + expectedBody: "Missing query parameter 'q'", + }, + { + name: "Wildcard search", + query: "*", + expectedStatus: http.StatusOK, + expectedBody: "No search results", + }, + { + name: "No matching results", + query: "nonexistent", + expectedStatus: http.StatusOK, + expectedBody: "No search results", + }, + { + name: "Matching result", + query: "example", + expectedStatus: http.StatusOK, + expectedBody: "total", + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + + req := httptest.NewRequest("GET", "/search?q="+tt.query, nil) + rec := httptest.NewRecorder() + + + SearchHandler(rec, req) + + + if rec.Code != tt.expectedStatus { + t.Errorf("expected status %v, got %v", tt.expectedStatus, rec.Code) + } + + + respBody, err := ioutil.ReadAll(rec.Body) + if err != nil { + t.Fatalf("Failed to read response body: %v", err) + } + bodyString := string(respBody) + + + if !strings.Contains(bodyString, tt.expectedBody) { + t.Errorf("expected body to contain %q, got %q", tt.expectedBody, bodyString) + } + }) + } +}