@@ -37,12 +37,25 @@ const char *reply_no_weather =
3737"HTTP/1.1 200 OK\r\n\r\n"
3838"{\"coord\":{\"lon\":10,\"lat\":50},\"main\":{\"temp\":20,\"pressure\":900,\"humidity\":60}}" ;
3939
40+ const char * owm_rain_reply =
41+ "HTTP/1.1 200 OK\r\n\r\n"
42+ "{"
43+ " \"coord\": {\"lon\": 11.0, \"lat\": 51.0},"
44+ " \"weather\": [{\"id\":500,\"main\":\"Rain\",\"description\":\"light rain\",\"icon\":\"10d\"}],"
45+ " \"main\": {\"temp\": 18.0, \"pressure\": 901, \"humidity\": 70},"
46+ " \"timezone\": 3600,"
47+ " \"sys\": {\"sunrise\":1604960000,\"sunset\":1605000000}"
48+ "}" ;
49+
50+ void Test_OpenWeatherMap_StaleWeatherStringsFollowLatestFullReply ();
51+
4052void Test_OpenWeatherMap () {
53+ weatherData_t * w ;
4154
4255 // HTTP header + json
4356 Weather_SetReply (owm_sample_reply );
4457
45- weatherData_t * w = Weather_GetData ();
58+ w = Weather_GetData ();
4659 SELFTEST_ASSERT_FLOATCOMPARE (w -> humidity , 85 );
4760 SELFTEST_ASSERT_FLOATCOMPARE (w -> pressure , 998 );
4861 SELFTEST_ASSERT_FLOATCOMPARE (w -> temp , 23 );
@@ -51,7 +64,50 @@ void Test_OpenWeatherMap() {
5164 SELFTEST_ASSERT_FLOATCOMPARE (w -> lat , 50 );
5265 SELFTEST_ASSERT_FLOATCOMPARE (w -> lon , 10 );
5366
67+ // Test with a response that has no "weather" array (edge case)
68+ Weather_SetReply (reply_no_weather );
69+ w = Weather_GetData ();
70+ // Numeric fields from "main" and "coord" must still parse
71+ SELFTEST_ASSERT_FLOATCOMPARE (w -> humidity , 60 );
72+ SELFTEST_ASSERT_FLOATCOMPARE (w -> pressure , 900 );
73+ SELFTEST_ASSERT_FLOATCOMPARE (w -> temp , 20 );
74+ SELFTEST_ASSERT_FLOATCOMPARE (w -> lat , 50 );
75+ SELFTEST_ASSERT_FLOATCOMPARE (w -> lon , 10 );
76+ // Current implementation only updates these strings when weather[0] exists,
77+ // so a reply without a weather array keeps the previous parsed values.
78+ SELFTEST_ASSERT_STRING (w -> main_weather , "Clear" );
79+ SELFTEST_ASSERT_STRING (w -> description , "clear sky" );
5480
81+ Test_OpenWeatherMap_StaleWeatherStringsFollowLatestFullReply ();
82+ }
83+
84+ void Test_OpenWeatherMap_StaleWeatherStringsFollowLatestFullReply () {
85+ weatherData_t * w ;
86+
87+ Weather_SetReply (owm_sample_reply );
88+ w = Weather_GetData ();
89+ SELFTEST_ASSERT_STRING (w -> main_weather , "Clear" );
90+ SELFTEST_ASSERT_STRING (w -> description , "clear sky" );
91+
92+ Weather_SetReply (owm_rain_reply );
93+ w = Weather_GetData ();
94+ SELFTEST_ASSERT_FLOATCOMPARE (w -> humidity , 70 );
95+ SELFTEST_ASSERT_FLOATCOMPARE (w -> pressure , 901 );
96+ SELFTEST_ASSERT_FLOATCOMPARE (w -> temp , 18 );
97+ SELFTEST_ASSERT_FLOATCOMPARE (w -> lat , 51 );
98+ SELFTEST_ASSERT_FLOATCOMPARE (w -> lon , 11 );
99+ SELFTEST_ASSERT_STRING (w -> main_weather , "Rain" );
100+ SELFTEST_ASSERT_STRING (w -> description , "light rain" );
101+
102+ Weather_SetReply (reply_no_weather );
103+ w = Weather_GetData ();
104+ SELFTEST_ASSERT_FLOATCOMPARE (w -> humidity , 60 );
105+ SELFTEST_ASSERT_FLOATCOMPARE (w -> pressure , 900 );
106+ SELFTEST_ASSERT_FLOATCOMPARE (w -> temp , 20 );
107+ SELFTEST_ASSERT_FLOATCOMPARE (w -> lat , 50 );
108+ SELFTEST_ASSERT_FLOATCOMPARE (w -> lon , 10 );
109+ SELFTEST_ASSERT_STRING (w -> main_weather , "Rain" );
110+ SELFTEST_ASSERT_STRING (w -> description , "light rain" );
55111}
56112
57113
0 commit comments