Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: upgrade to v2 #193

Merged
merged 1 commit into from
Sep 7, 2023
Merged

feat: upgrade to v2 #193

merged 1 commit into from
Sep 7, 2023

Conversation

charankamarapu
Copy link
Member

Upgrading go-sdk v1 to go-sdk v2

Changes Made :

  1. Cleaned unused code like dependency wrappers etc
  2. Added mock/stub feature
  3. Changed package from github.com/keploy/go-sdk to github.com/keploy/go-sdk/v2

How to test Mock/stub feature :

  1. Install keploy binary considering the changes in this PR
  2. Setup gin-mongo app and import go-sdk
  3. Add given unit test file in main_test.go for recording Mock
package main

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

	"github.com/gin-gonic/gin"
	"github.com/keploy/go-sdk/v2/mock"
	"github.com/keploy/go-sdk/v2/pkg/keploy"
)

func setup() {
	mock.NewContext(mock.Config{
		TestSuite:        "test-set-5", // testSuiteName
		Mode:             keploy.MODE_RECORD, // Mode
		Path:             "/home/ubuntu/dont_touch/samples-go/gin-mongo", // Path to store stubs
		EnableKeployLogs: false,
	})
	dbName, collection := "keploy", "url-shortener"
	client, err := New("localhost:27017", dbName)
	if err != nil {
		panic("Failed to initialize MongoDB: " + err.Error())
	}
	db := client.Database(dbName)
	col = db.Collection(collection)
}

func TestGetURL(t *testing.T) {
	// Setting up Gin and routes
	r := gin.Default()
	r.GET("/:param", getURL)
	r.POST("/url", putURL)

	// Assuming we already have a shortened URL stored with the hash "test123"
	req, err := http.NewRequest(http.MethodGet, "https://www.example.com/Lhr4BWAi", nil)
	if err != nil {
		t.Fatalf("Couldn't create request: %v\n", err)
	}

	w := httptest.NewRecorder()

	r.ServeHTTP(w, req)

	// We're just checking if it can successfully redirect
	if w.Code != http.StatusSeeOther {
		t.Fatalf("Expected HTTP 303 See Other, but got %v", w.Code)
	}
}

func TestPutURL(t *testing.T) {
	setup()
	r := gin.Default()
	r.GET("/:param", getURL)
	r.POST("/url", putURL)

	data := map[string]string{
		"url": "https://www.example.com",
	}
	payload, err := json.Marshal(data)
	if err != nil {
		t.Fatalf("rrdfe: %v\n", err)
	}

	req, err := http.NewRequest(http.MethodPost, "/url", bytes.NewBuffer(payload))
	if err != nil {
		t.Fatalf("Couldn't create request: %v\n", err)
	}
	req.Header.Set("Content-Type", "application/json")

	w := httptest.NewRecorder()
	r.ServeHTTP(w, req)

	// Checking if the URL was successfully shortened and stored
	if w.Code != http.StatusOK {
		t.Fatalf("Expected HTTP 200 OK, but got %v", w.Code)
	}

	var response map[string]interface{}
	err = json.Unmarshal(w.Body.Bytes(), &response)
	if err != nil {
		t.Fatalf("Failed to unmarshal response: %v\n", err)
	}
	fmt.Println("response-url" + response["url"].(string))

	if response["url"] == nil || response["ts"] == nil {
		t.Fatalf("Response did not contain expected fields")
	}
	mock.KillProcessOnPort()
}
  1. Make sure your database is running and then run the test file . Once the test is done mocks will be created in the path mentioned.
  2. To run in test mode Change the mode to MODE_TEST, turn off the database and run the tests.

Signed-off-by: charankamarapu <kamarapucharan@gmail.com>
@slayerjain slayerjain merged commit c21ea44 into keploy:main Sep 7, 2023
1 check passed
@charankamarapu charankamarapu deleted the sdk-v2 branch September 25, 2023 18:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants