forked from openpredictionmarkets/socialpredict
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Creating setuphandler to allow in-game set up to be queried via API (o…
…penpredictionmarkets#274) * Updating draft handler. * Update handler with test function. * Tested working endpoint and golang test function for happy case. * Updating, removing comment.
- Loading branch information
Showing
4 changed files
with
118 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package setup | ||
|
||
import ( | ||
"encoding/json" | ||
"net/http" | ||
"socialpredict/setup" | ||
) | ||
|
||
func GetSetupHandler(w http.ResponseWriter, r *http.Request) { | ||
|
||
appConfig, err := setup.LoadEconomicsConfig() | ||
if err != nil { | ||
http.Error(w, "Failed to load economic config", http.StatusInternalServerError) | ||
return | ||
} | ||
|
||
w.Header().Set("Content-Type", "application/json") | ||
err = json.NewEncoder(w).Encode(appConfig.Economics) | ||
if err != nil { | ||
http.Error(w, "Failed to encode response", http.StatusInternalServerError) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
package setup | ||
|
||
import ( | ||
"encoding/json" | ||
"net/http" | ||
"net/http/httptest" | ||
"socialpredict/setup" | ||
"strings" | ||
"testing" | ||
) | ||
|
||
var loadEconomicsConfig = setup.LoadEconomicsConfig | ||
|
||
func TestGetSetupHandler(t *testing.T) { | ||
tests := []struct { | ||
Name string | ||
MockConfigLoader func() (*setup.EconomicConfig, error) | ||
ExpectedStatus int | ||
ExpectedResponse string | ||
IsJSONResponse bool | ||
}{ | ||
{ | ||
Name: "successful load", | ||
MockConfigLoader: func() (*setup.EconomicConfig, error) { | ||
return setup.LoadEconomicsConfig() | ||
}, | ||
ExpectedStatus: http.StatusOK, | ||
ExpectedResponse: `{ | ||
"MarketCreation":{"InitialMarketProbability":0.5,"InitialMarketSubsidization":10,"InitialMarketYes":0,"InitialMarketNo":0}, | ||
"MarketIncentives":{"CreateMarketCost":10,"TraderBonus":1}, | ||
"User":{"InitialAccountBalance":0,"MaximumDebtAllowed":500}, | ||
"Betting":{"MinimumBet":1,"BetFees":{"InitialBetFee":1,"EachBetFee":0,"SellSharesFee":0}}}`, | ||
IsJSONResponse: true, | ||
}, | ||
} | ||
|
||
for _, test := range tests { | ||
t.Run(test.Name, func(t *testing.T) { | ||
// Replace the actual loader function with the mock | ||
loadEconomicsConfig = test.MockConfigLoader | ||
defer func() { loadEconomicsConfig = setup.LoadEconomicsConfig }() | ||
|
||
req, err := http.NewRequest("GET", "/setup", nil) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
rr := httptest.NewRecorder() | ||
handler := http.HandlerFunc(GetSetupHandler) | ||
|
||
handler.ServeHTTP(rr, req) | ||
|
||
if status := rr.Code; status != test.ExpectedStatus { | ||
t.Errorf("%s: handler returned wrong status code: got %v want %v", | ||
test.Name, status, test.ExpectedStatus) | ||
} | ||
|
||
if test.IsJSONResponse { | ||
var expectedjson, actualjson map[string]interface{} | ||
if err := json.Unmarshal([]byte(test.ExpectedResponse), &expectedjson); err != nil { | ||
t.Fatalf("%s: error parsing expected response JSON: %v", test.Name, err) | ||
} | ||
if err := json.Unmarshal(rr.Body.Bytes(), &actualjson); err != nil { | ||
t.Fatalf("%s: error parsing actual response JSON: %v", test.Name, err) | ||
} | ||
|
||
if !jsonEqual(expectedjson, actualjson) { | ||
t.Errorf("%s: handler returned unexpected body: got %v want %v", | ||
test.Name, rr.Body.String(), test.ExpectedResponse) | ||
} | ||
} else { | ||
if strings.TrimSpace(rr.Body.String()) != strings.TrimSpace(test.ExpectedResponse) { | ||
t.Errorf("%s: handler returned unexpected body: got %v want %v", | ||
test.Name, rr.Body.String(), test.ExpectedResponse) | ||
} | ||
} | ||
}) | ||
} | ||
} | ||
|
||
// jsonEqual compares two JSON objects for equality | ||
func jsonEqual(a, b map[string]interface{}) bool { | ||
return jsonString(a) == jsonString(b) | ||
} | ||
|
||
// jsonString converts a JSON object to a sorted string | ||
func jsonString(v interface{}) string { | ||
bytes, _ := json.Marshal(v) | ||
return string(bytes) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters