From 44efcabb0683bdf39f2045a9c7af0af9b915f99d Mon Sep 17 00:00:00 2001 From: Agniva De Sarker Date: Wed, 15 Jun 2016 12:29:01 +0530 Subject: [PATCH] Checking for invalid category --- gomdb.go | 10 +++++++++- gomdb_test.go | 22 ++++++++++++++++++++++ 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/gomdb.go b/gomdb.go index 4a72693..564d522 100644 --- a/gomdb.go +++ b/gomdb.go @@ -151,10 +151,18 @@ func MovieByImdbID(id string) (*MovieResult, error) { func requestAPI(apiCategory string, params ...string) (resp *http.Response, err error) { var URL *url.URL URL, err = url.Parse(baseURL) - if err != nil { return nil, err } + + // Checking for invalid category + if len(params) > 1 && params[2] != "" { + if params[2] != MovieSearch && + params[2] != SeriesSearch && + params[2] != EpisodeSearch { + return nil, errors.New("Invalid search category- " + params[2]) + } + } URL.Path += "/" parameters := url.Values{} switch apiCategory { diff --git a/gomdb_test.go b/gomdb_test.go index 42a53fd..4a26515 100644 --- a/gomdb_test.go +++ b/gomdb_test.go @@ -61,6 +61,28 @@ func TestFailSearch(t *testing.T) { } } +func TestInvalidCategory(t *testing.T) { + tests := []struct { + query *QueryData + }{ + {&QueryData{Title: "Game of Thrones", Year: "2001", SearchType: "bad"}}, + {&QueryData{Title: "Dexter", SearchType: "bad"}}, + } + + for i, item := range tests { + _, err := Search(item.query) + if err == nil { + t.Errorf("Test[%d]: Got nil error", i) + continue + } + // Checking for strings is bad. But the error type is formatted + if err.Error() != "Invalid search category- bad" { + t.Errorf("Test[%d]: Unexpected value- %s, Got- %s", i, err) + continue + } + } +} + func TestMovieByTitle(t *testing.T) { tests := []struct { query *QueryData