Skip to content

Commit

Permalink
test(getShortURL): base test for HTTP get on a short URL (#22)
Browse files Browse the repository at this point in the history
* test(getShortURL): base test for HTTP get on a short URL
  • Loading branch information
logan-bobo authored Aug 26, 2024
1 parent 23759dc commit 1930b86
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 3 deletions.
6 changes: 4 additions & 2 deletions docker-compose-test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,14 @@ services:
volumes:
- pgdatatest:/var/lib/postgresql/data
- ./bin/local-init.sql:/docker-entrypoint-initdb.d/local-init.sql
ports:
- 5002:5432
healthcheck:
test: ["CMD-SHELL", "pg_isready"]
interval: 2s
timeout: 5s
retries: 10
redis:
image: 'bitnami/redis:latest'
environment:
- ALLOW_EMPTY_PASSWORD=yes
volumes:
pgdatatest:
97 changes: 96 additions & 1 deletion handlers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"testing"

"github.com/pressly/goose/v3"
"github.com/redis/go-redis/v9"
"golang.org/x/crypto/bcrypt"

"url-short/internal/database"
Expand All @@ -25,7 +26,7 @@ var (
userBadInput = []byte(`{"gmail":"[email protected]", "auth": "test", "extra_data": "data"}`)
userBadEmail = []byte(`{"email": "test1mail.com", "password": "test"}`)

longUrl = []byte(`{"long_url":"www.google.com"}`)
longUrl = []byte(`{"long_url":"https://www.google.com"}`)
)

func resetDB(db *sql.DB) error {
Expand Down Expand Up @@ -568,3 +569,97 @@ func TestPostLongURL(t *testing.T) {
}
})
}

func TestGetShortURL(t *testing.T) {
dbURL := os.Getenv("PG_CONN")
db, err := sql.Open("postgres", dbURL)
rdbURL := os.Getenv("RDB_CONN")

if err != nil {
t.Errorf("can not open database connection")
}

defer db.Close()

err = resetDB(db)

if err != nil {
t.Errorf("could not resetDB %q", err)
}

dbQueries := database.New(db)

opt, err := redis.ParseURL(rdbURL)

if err != nil {
t.Errorf("oculd not parse reddis connection")
}

redisClient := redis.NewClient(opt)

defer redisClient.Close()

apiCfg := apiConfig{
DB: dbQueries,
RDB: redisClient,
}

_, err = setupUserOne(&apiCfg)

if err != nil {
t.Errorf("can not set up user for test case with err %q", err)
}

userOne, err := loginUserOne(&apiCfg)

if err != nil {
t.Errorf("can not login user one for test case with err %q", err)
}

postLongURLRequest := httptest.NewRequest(
http.MethodPost,
"/api/v1/data/shorten",
bytes.NewBuffer(longUrl),
)

buildHeader := fmt.Sprintf("Bearer %s", userOne.RefreshToken)
postLongURLRequest.Header.Set("Authorization", buildHeader)

postURLResponse := httptest.NewRecorder()

user, err := dbQueries.SelectUser(postLongURLRequest.Context(), userOne.Email)

if err != nil {
t.Error("could not find user that was expected to exist")
}

apiCfg.postLongURL(postURLResponse, postLongURLRequest, user)

gotPutLongURL := LongURLResponse{}

_ = json.NewDecoder(postURLResponse.Body).Decode(&gotPutLongURL)

t.Run("test short url redirects to a long URL", func(t *testing.T) {
getShortURLRequest := httptest.NewRequest(
http.MethodGet,
fmt.Sprintf("/api/v1/%s", gotPutLongURL.ShortURL),
http.NoBody,
)

getShortURLRequest.SetPathValue("shortUrl", gotPutLongURL.ShortURL)

getShortURLResponse := httptest.NewRecorder()

apiCfg.getShortURL(getShortURLResponse, getShortURLRequest)

redirectLocation := getShortURLResponse.Result().Header.Get("Location")

if redirectLocation != "https://www.google.com" {
t.Errorf(
"incorrect redirect to longURL got %q wanted %q",
redirectLocation,
"https://www.google.com",
)
}
})
}

0 comments on commit 1930b86

Please sign in to comment.