Commit 073ddfbe authored by Strakhov Egor's avatar Strakhov Egor

Added Push Document Test

parent 2bc68685
...@@ -8,7 +8,6 @@ import ( ...@@ -8,7 +8,6 @@ import (
"time" "time"
"github.com/gorilla/mux" "github.com/gorilla/mux"
"go.mongodb.org/mongo-driver/mongo/options" "go.mongodb.org/mongo-driver/mongo/options"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/primitive" "go.mongodb.org/mongo-driver/bson/primitive"
"go.mongodb.org/mongo-driver/mongo" "go.mongodb.org/mongo-driver/mongo"
) )
...@@ -55,29 +54,6 @@ func CreateKick(response http.ResponseWriter, request *http.Request) { ...@@ -55,29 +54,6 @@ func CreateKick(response http.ResponseWriter, request *http.Request) {
} }
} }
func GetKicks(response http.ResponseWriter, request *http.Request) {
response.Header().Set("content-type", "application/json")
var kicks []Kick
collection := client.Database("mongodb").Collection("kicks")
ctx, _ := context.WithTimeout(context.Background(), 30*time.Second)
cursor, err := collection.Find(ctx, bson.M{})
if err != nil {
response.WriteHeader(http.StatusInternalServerError)
response.Write([]byte(`{ "message": "` + err.Error() + `" }`))
}
defer cursor.Close(ctx)
for cursor.Next(ctx) {
var kick Kick
cursor.Decode(&kick)
kicks = append(kicks, kick)
}
if err := cursor.Err(); err != nil {
response.WriteHeader(http.StatusInternalServerError)
response.Write([]byte(`{ "message": "` + err.Error() + `" }`))
}
json.NewEncoder(response).Encode(kicks)
}
func main() { func main() {
fmt.Println("Starting the application...") fmt.Println("Starting the application...")
ctx, _ := context.WithTimeout(context.Background(), 10*time.Second) ctx, _ := context.WithTimeout(context.Background(), 10*time.Second)
...@@ -85,6 +61,5 @@ func main() { ...@@ -85,6 +61,5 @@ func main() {
client, _ = mongo.Connect(ctx, clientOptions) client, _ = mongo.Connect(ctx, clientOptions)
router := mux.NewRouter() router := mux.NewRouter()
router.HandleFunc("/kick", CreateKick).Methods("POST") router.HandleFunc("/kick", CreateKick).Methods("POST")
router.HandleFunc("/kicks", GetKicks).Methods("GET")
http.ListenAndServe(":8080", router) http.ListenAndServe(":8080", router)
} }
...@@ -26,7 +26,6 @@ func init() { ...@@ -26,7 +26,6 @@ func init() {
func listen(finished chan bool) { func listen(finished chan bool) {
router := mux.NewRouter() router := mux.NewRouter()
router.HandleFunc("/kick", CreateKick).Methods("POST") router.HandleFunc("/kick", CreateKick).Methods("POST")
router.HandleFunc("/kicks", GetKicks).Methods("GET")
finished <- true finished <- true
http.ListenAndServe(":8080", router) http.ListenAndServe(":8080", router)
...@@ -49,7 +48,6 @@ func sendJson(url string) { ...@@ -49,7 +48,6 @@ func sendJson(url string) {
var initial Kick var initial Kick
file, _ := ioutil.ReadFile("test.json") file, _ := ioutil.ReadFile("test.json")
_ = json.Unmarshal([]byte(file), &initial) _ = json.Unmarshal([]byte(file), &initial)
log.Printf("%v\n", initial)
//posting json //posting json
r, err := os.Open("test.json") r, err := os.Open("test.json")
...@@ -97,28 +95,7 @@ func sendJson(url string) { ...@@ -97,28 +95,7 @@ func sendJson(url string) {
} }
func getJsons() {
resp, err := http.Get("http://localhost:8080/kicks")
if err != nil {
log.Fatalf("Cannot Get jsons")
}
if resp.StatusCode != 200 {
log.Fatalf("%v\n", resp.Status)
}
}
func TestMain(t *testing.T) { func TestMain(t *testing.T) {
sendJson("http://localhost:8080/kick") sendJson("http://localhost:8080/kick")
getJsons()
}
/*
func TestCreation(t *testing.T) {
sendJson("http://localhost:8080/kick")
} }
*/
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment