173 lines
4.4 KiB
JavaScript
Raw Normal View History

global.moment = require("moment-timezone");
const { performWebRequest, formatTime } = require("../../../../modules/default/utils");
New weather provider: Yr.no (#2948) # Added Yr.no as a weather provider Yr.no is a free Norwegian weather service. The configuration is quite simple: ```js { weatherProvider: "yr", lat: 59.9171, lon: 10.7276, altitude: 30 } ``` The latitude and longitude cannot have more than 4 decimals, but that should be plenty. To quote yr: "There is no need to ask for weather forecasts with nanometer precision!". The altitude should be meters above sea level and defaults to 0. If `type` is set to `current` the symbol can display the next 1, 6 or 12 hours by setting `currentForecastHours` (default is 1). It states in [Getting started-guide](https://developer.yr.no/doc/GettingStarted/) that users of the API should cache the results and use the `Expires`-header to know when to ask for new data. By using the `If-Modified-Since`-header we can avoid downloading the same data over and over again. I chose not to override the `User-Agent`-header set in [`server.js`](https://github.com/MichMich/MagicMirror/blob/a328ce5/js/server.js#L97) even though it does not comply with [the terms of service](https://developer.yr.no/doc/TermsOfService/). It currently works with the default header, and by searching the web for MagicMirror the GitHub-repo should be easy to find without an explicit link. I also had to make some minor changes to `server.js` and `weatherprovider.js` to be able to send and return HTTP headers. To handle the HTTP 304 response without body I chose to return `undefined` so we easily can use the response as a condition: `if (response) ...`. The documentation for the API is available here: - [API Reference overview](https://api.met.no/weatherapi/) - [Locationforecast](https://api.met.no/weatherapi/locationforecast/2.0/) - Used to get the weather forecast - [Sunrise](https://api.met.no/weatherapi/sunrise/2.0/documentation) - used to find sunrise and sunset times Co-authored-by: Veeck <github@veeck.de>
2022-11-08 06:18:47 +01:00
const nodeVersion = process.version.match(/^v(\d+)\.*/)[1];
describe("Default modules utils tests", () => {
describe("performWebRequest", () => {
New weather provider: Yr.no (#2948) # Added Yr.no as a weather provider Yr.no is a free Norwegian weather service. The configuration is quite simple: ```js { weatherProvider: "yr", lat: 59.9171, lon: 10.7276, altitude: 30 } ``` The latitude and longitude cannot have more than 4 decimals, but that should be plenty. To quote yr: "There is no need to ask for weather forecasts with nanometer precision!". The altitude should be meters above sea level and defaults to 0. If `type` is set to `current` the symbol can display the next 1, 6 or 12 hours by setting `currentForecastHours` (default is 1). It states in [Getting started-guide](https://developer.yr.no/doc/GettingStarted/) that users of the API should cache the results and use the `Expires`-header to know when to ask for new data. By using the `If-Modified-Since`-header we can avoid downloading the same data over and over again. I chose not to override the `User-Agent`-header set in [`server.js`](https://github.com/MichMich/MagicMirror/blob/a328ce5/js/server.js#L97) even though it does not comply with [the terms of service](https://developer.yr.no/doc/TermsOfService/). It currently works with the default header, and by searching the web for MagicMirror the GitHub-repo should be easy to find without an explicit link. I also had to make some minor changes to `server.js` and `weatherprovider.js` to be able to send and return HTTP headers. To handle the HTTP 304 response without body I chose to return `undefined` so we easily can use the response as a condition: `if (response) ...`. The documentation for the API is available here: - [API Reference overview](https://api.met.no/weatherapi/) - [Locationforecast](https://api.met.no/weatherapi/locationforecast/2.0/) - Used to get the weather forecast - [Sunrise](https://api.met.no/weatherapi/sunrise/2.0/documentation) - used to find sunrise and sunset times Co-authored-by: Veeck <github@veeck.de>
2022-11-08 06:18:47 +01:00
if (nodeVersion > 18) {
const locationHost = "localhost:8080";
const locationProtocol = "http";
let fetchResponse;
let fetchMock;
let urlToCall;
New weather provider: Yr.no (#2948) # Added Yr.no as a weather provider Yr.no is a free Norwegian weather service. The configuration is quite simple: ```js { weatherProvider: "yr", lat: 59.9171, lon: 10.7276, altitude: 30 } ``` The latitude and longitude cannot have more than 4 decimals, but that should be plenty. To quote yr: "There is no need to ask for weather forecasts with nanometer precision!". The altitude should be meters above sea level and defaults to 0. If `type` is set to `current` the symbol can display the next 1, 6 or 12 hours by setting `currentForecastHours` (default is 1). It states in [Getting started-guide](https://developer.yr.no/doc/GettingStarted/) that users of the API should cache the results and use the `Expires`-header to know when to ask for new data. By using the `If-Modified-Since`-header we can avoid downloading the same data over and over again. I chose not to override the `User-Agent`-header set in [`server.js`](https://github.com/MichMich/MagicMirror/blob/a328ce5/js/server.js#L97) even though it does not comply with [the terms of service](https://developer.yr.no/doc/TermsOfService/). It currently works with the default header, and by searching the web for MagicMirror the GitHub-repo should be easy to find without an explicit link. I also had to make some minor changes to `server.js` and `weatherprovider.js` to be able to send and return HTTP headers. To handle the HTTP 304 response without body I chose to return `undefined` so we easily can use the response as a condition: `if (response) ...`. The documentation for the API is available here: - [API Reference overview](https://api.met.no/weatherapi/) - [Locationforecast](https://api.met.no/weatherapi/locationforecast/2.0/) - Used to get the weather forecast - [Sunrise](https://api.met.no/weatherapi/sunrise/2.0/documentation) - used to find sunrise and sunset times Co-authored-by: Veeck <github@veeck.de>
2022-11-08 06:18:47 +01:00
beforeEach(() => {
fetchResponse = new Response();
global.fetch = jest.fn(() => Promise.resolve(fetchResponse));
fetchMock = global.fetch;
});
describe("When using cors proxy", () => {
Object.defineProperty(global, "location", {
value: {
host: locationHost,
protocol: locationProtocol
}
});
test("Calls correct URL once", async () => {
urlToCall = "http://www.test.com/path?param1=value1";
New weather provider: Yr.no (#2948) # Added Yr.no as a weather provider Yr.no is a free Norwegian weather service. The configuration is quite simple: ```js { weatherProvider: "yr", lat: 59.9171, lon: 10.7276, altitude: 30 } ``` The latitude and longitude cannot have more than 4 decimals, but that should be plenty. To quote yr: "There is no need to ask for weather forecasts with nanometer precision!". The altitude should be meters above sea level and defaults to 0. If `type` is set to `current` the symbol can display the next 1, 6 or 12 hours by setting `currentForecastHours` (default is 1). It states in [Getting started-guide](https://developer.yr.no/doc/GettingStarted/) that users of the API should cache the results and use the `Expires`-header to know when to ask for new data. By using the `If-Modified-Since`-header we can avoid downloading the same data over and over again. I chose not to override the `User-Agent`-header set in [`server.js`](https://github.com/MichMich/MagicMirror/blob/a328ce5/js/server.js#L97) even though it does not comply with [the terms of service](https://developer.yr.no/doc/TermsOfService/). It currently works with the default header, and by searching the web for MagicMirror the GitHub-repo should be easy to find without an explicit link. I also had to make some minor changes to `server.js` and `weatherprovider.js` to be able to send and return HTTP headers. To handle the HTTP 304 response without body I chose to return `undefined` so we easily can use the response as a condition: `if (response) ...`. The documentation for the API is available here: - [API Reference overview](https://api.met.no/weatherapi/) - [Locationforecast](https://api.met.no/weatherapi/locationforecast/2.0/) - Used to get the weather forecast - [Sunrise](https://api.met.no/weatherapi/sunrise/2.0/documentation) - used to find sunrise and sunset times Co-authored-by: Veeck <github@veeck.de>
2022-11-08 06:18:47 +01:00
await performWebRequest(urlToCall, "json", true);
New weather provider: Yr.no (#2948) # Added Yr.no as a weather provider Yr.no is a free Norwegian weather service. The configuration is quite simple: ```js { weatherProvider: "yr", lat: 59.9171, lon: 10.7276, altitude: 30 } ``` The latitude and longitude cannot have more than 4 decimals, but that should be plenty. To quote yr: "There is no need to ask for weather forecasts with nanometer precision!". The altitude should be meters above sea level and defaults to 0. If `type` is set to `current` the symbol can display the next 1, 6 or 12 hours by setting `currentForecastHours` (default is 1). It states in [Getting started-guide](https://developer.yr.no/doc/GettingStarted/) that users of the API should cache the results and use the `Expires`-header to know when to ask for new data. By using the `If-Modified-Since`-header we can avoid downloading the same data over and over again. I chose not to override the `User-Agent`-header set in [`server.js`](https://github.com/MichMich/MagicMirror/blob/a328ce5/js/server.js#L97) even though it does not comply with [the terms of service](https://developer.yr.no/doc/TermsOfService/). It currently works with the default header, and by searching the web for MagicMirror the GitHub-repo should be easy to find without an explicit link. I also had to make some minor changes to `server.js` and `weatherprovider.js` to be able to send and return HTTP headers. To handle the HTTP 304 response without body I chose to return `undefined` so we easily can use the response as a condition: `if (response) ...`. The documentation for the API is available here: - [API Reference overview](https://api.met.no/weatherapi/) - [Locationforecast](https://api.met.no/weatherapi/locationforecast/2.0/) - Used to get the weather forecast - [Sunrise](https://api.met.no/weatherapi/sunrise/2.0/documentation) - used to find sunrise and sunset times Co-authored-by: Veeck <github@veeck.de>
2022-11-08 06:18:47 +01:00
expect(fetchMock.mock.calls.length).toBe(1);
expect(fetchMock.mock.calls[0][0]).toBe(`${locationProtocol}//${locationHost}/cors?url=${urlToCall}`);
});
test("Sends correct headers", async () => {
urlToCall = "http://www.test.com/path?param1=value1";
New weather provider: Yr.no (#2948) # Added Yr.no as a weather provider Yr.no is a free Norwegian weather service. The configuration is quite simple: ```js { weatherProvider: "yr", lat: 59.9171, lon: 10.7276, altitude: 30 } ``` The latitude and longitude cannot have more than 4 decimals, but that should be plenty. To quote yr: "There is no need to ask for weather forecasts with nanometer precision!". The altitude should be meters above sea level and defaults to 0. If `type` is set to `current` the symbol can display the next 1, 6 or 12 hours by setting `currentForecastHours` (default is 1). It states in [Getting started-guide](https://developer.yr.no/doc/GettingStarted/) that users of the API should cache the results and use the `Expires`-header to know when to ask for new data. By using the `If-Modified-Since`-header we can avoid downloading the same data over and over again. I chose not to override the `User-Agent`-header set in [`server.js`](https://github.com/MichMich/MagicMirror/blob/a328ce5/js/server.js#L97) even though it does not comply with [the terms of service](https://developer.yr.no/doc/TermsOfService/). It currently works with the default header, and by searching the web for MagicMirror the GitHub-repo should be easy to find without an explicit link. I also had to make some minor changes to `server.js` and `weatherprovider.js` to be able to send and return HTTP headers. To handle the HTTP 304 response without body I chose to return `undefined` so we easily can use the response as a condition: `if (response) ...`. The documentation for the API is available here: - [API Reference overview](https://api.met.no/weatherapi/) - [Locationforecast](https://api.met.no/weatherapi/locationforecast/2.0/) - Used to get the weather forecast - [Sunrise](https://api.met.no/weatherapi/sunrise/2.0/documentation) - used to find sunrise and sunset times Co-authored-by: Veeck <github@veeck.de>
2022-11-08 06:18:47 +01:00
const headers = [
{ name: "header1", value: "value1" },
{ name: "header2", value: "value2" }
];
await performWebRequest(urlToCall, "json", true, headers);
New weather provider: Yr.no (#2948) # Added Yr.no as a weather provider Yr.no is a free Norwegian weather service. The configuration is quite simple: ```js { weatherProvider: "yr", lat: 59.9171, lon: 10.7276, altitude: 30 } ``` The latitude and longitude cannot have more than 4 decimals, but that should be plenty. To quote yr: "There is no need to ask for weather forecasts with nanometer precision!". The altitude should be meters above sea level and defaults to 0. If `type` is set to `current` the symbol can display the next 1, 6 or 12 hours by setting `currentForecastHours` (default is 1). It states in [Getting started-guide](https://developer.yr.no/doc/GettingStarted/) that users of the API should cache the results and use the `Expires`-header to know when to ask for new data. By using the `If-Modified-Since`-header we can avoid downloading the same data over and over again. I chose not to override the `User-Agent`-header set in [`server.js`](https://github.com/MichMich/MagicMirror/blob/a328ce5/js/server.js#L97) even though it does not comply with [the terms of service](https://developer.yr.no/doc/TermsOfService/). It currently works with the default header, and by searching the web for MagicMirror the GitHub-repo should be easy to find without an explicit link. I also had to make some minor changes to `server.js` and `weatherprovider.js` to be able to send and return HTTP headers. To handle the HTTP 304 response without body I chose to return `undefined` so we easily can use the response as a condition: `if (response) ...`. The documentation for the API is available here: - [API Reference overview](https://api.met.no/weatherapi/) - [Locationforecast](https://api.met.no/weatherapi/locationforecast/2.0/) - Used to get the weather forecast - [Sunrise](https://api.met.no/weatherapi/sunrise/2.0/documentation) - used to find sunrise and sunset times Co-authored-by: Veeck <github@veeck.de>
2022-11-08 06:18:47 +01:00
expect(fetchMock.mock.calls.length).toBe(1);
expect(fetchMock.mock.calls[0][0]).toBe(`${locationProtocol}//${locationHost}/cors?sendheaders=header1:value1,header2:value2&url=${urlToCall}`);
});
});
describe("When not using cors proxy", () => {
test("Calls correct URL once", async () => {
urlToCall = "http://www.test.com/path?param1=value1";
New weather provider: Yr.no (#2948) # Added Yr.no as a weather provider Yr.no is a free Norwegian weather service. The configuration is quite simple: ```js { weatherProvider: "yr", lat: 59.9171, lon: 10.7276, altitude: 30 } ``` The latitude and longitude cannot have more than 4 decimals, but that should be plenty. To quote yr: "There is no need to ask for weather forecasts with nanometer precision!". The altitude should be meters above sea level and defaults to 0. If `type` is set to `current` the symbol can display the next 1, 6 or 12 hours by setting `currentForecastHours` (default is 1). It states in [Getting started-guide](https://developer.yr.no/doc/GettingStarted/) that users of the API should cache the results and use the `Expires`-header to know when to ask for new data. By using the `If-Modified-Since`-header we can avoid downloading the same data over and over again. I chose not to override the `User-Agent`-header set in [`server.js`](https://github.com/MichMich/MagicMirror/blob/a328ce5/js/server.js#L97) even though it does not comply with [the terms of service](https://developer.yr.no/doc/TermsOfService/). It currently works with the default header, and by searching the web for MagicMirror the GitHub-repo should be easy to find without an explicit link. I also had to make some minor changes to `server.js` and `weatherprovider.js` to be able to send and return HTTP headers. To handle the HTTP 304 response without body I chose to return `undefined` so we easily can use the response as a condition: `if (response) ...`. The documentation for the API is available here: - [API Reference overview](https://api.met.no/weatherapi/) - [Locationforecast](https://api.met.no/weatherapi/locationforecast/2.0/) - Used to get the weather forecast - [Sunrise](https://api.met.no/weatherapi/sunrise/2.0/documentation) - used to find sunrise and sunset times Co-authored-by: Veeck <github@veeck.de>
2022-11-08 06:18:47 +01:00
await performWebRequest(urlToCall);
New weather provider: Yr.no (#2948) # Added Yr.no as a weather provider Yr.no is a free Norwegian weather service. The configuration is quite simple: ```js { weatherProvider: "yr", lat: 59.9171, lon: 10.7276, altitude: 30 } ``` The latitude and longitude cannot have more than 4 decimals, but that should be plenty. To quote yr: "There is no need to ask for weather forecasts with nanometer precision!". The altitude should be meters above sea level and defaults to 0. If `type` is set to `current` the symbol can display the next 1, 6 or 12 hours by setting `currentForecastHours` (default is 1). It states in [Getting started-guide](https://developer.yr.no/doc/GettingStarted/) that users of the API should cache the results and use the `Expires`-header to know when to ask for new data. By using the `If-Modified-Since`-header we can avoid downloading the same data over and over again. I chose not to override the `User-Agent`-header set in [`server.js`](https://github.com/MichMich/MagicMirror/blob/a328ce5/js/server.js#L97) even though it does not comply with [the terms of service](https://developer.yr.no/doc/TermsOfService/). It currently works with the default header, and by searching the web for MagicMirror the GitHub-repo should be easy to find without an explicit link. I also had to make some minor changes to `server.js` and `weatherprovider.js` to be able to send and return HTTP headers. To handle the HTTP 304 response without body I chose to return `undefined` so we easily can use the response as a condition: `if (response) ...`. The documentation for the API is available here: - [API Reference overview](https://api.met.no/weatherapi/) - [Locationforecast](https://api.met.no/weatherapi/locationforecast/2.0/) - Used to get the weather forecast - [Sunrise](https://api.met.no/weatherapi/sunrise/2.0/documentation) - used to find sunrise and sunset times Co-authored-by: Veeck <github@veeck.de>
2022-11-08 06:18:47 +01:00
expect(fetchMock.mock.calls.length).toBe(1);
expect(fetchMock.mock.calls[0][0]).toBe(urlToCall);
});
test("Sends correct headers", async () => {
urlToCall = "http://www.test.com/path?param1=value1";
New weather provider: Yr.no (#2948) # Added Yr.no as a weather provider Yr.no is a free Norwegian weather service. The configuration is quite simple: ```js { weatherProvider: "yr", lat: 59.9171, lon: 10.7276, altitude: 30 } ``` The latitude and longitude cannot have more than 4 decimals, but that should be plenty. To quote yr: "There is no need to ask for weather forecasts with nanometer precision!". The altitude should be meters above sea level and defaults to 0. If `type` is set to `current` the symbol can display the next 1, 6 or 12 hours by setting `currentForecastHours` (default is 1). It states in [Getting started-guide](https://developer.yr.no/doc/GettingStarted/) that users of the API should cache the results and use the `Expires`-header to know when to ask for new data. By using the `If-Modified-Since`-header we can avoid downloading the same data over and over again. I chose not to override the `User-Agent`-header set in [`server.js`](https://github.com/MichMich/MagicMirror/blob/a328ce5/js/server.js#L97) even though it does not comply with [the terms of service](https://developer.yr.no/doc/TermsOfService/). It currently works with the default header, and by searching the web for MagicMirror the GitHub-repo should be easy to find without an explicit link. I also had to make some minor changes to `server.js` and `weatherprovider.js` to be able to send and return HTTP headers. To handle the HTTP 304 response without body I chose to return `undefined` so we easily can use the response as a condition: `if (response) ...`. The documentation for the API is available here: - [API Reference overview](https://api.met.no/weatherapi/) - [Locationforecast](https://api.met.no/weatherapi/locationforecast/2.0/) - Used to get the weather forecast - [Sunrise](https://api.met.no/weatherapi/sunrise/2.0/documentation) - used to find sunrise and sunset times Co-authored-by: Veeck <github@veeck.de>
2022-11-08 06:18:47 +01:00
const headers = [
{ name: "header1", value: "value1" },
{ name: "header2", value: "value2" }
];
await performWebRequest(urlToCall, "json", false, headers);
New weather provider: Yr.no (#2948) # Added Yr.no as a weather provider Yr.no is a free Norwegian weather service. The configuration is quite simple: ```js { weatherProvider: "yr", lat: 59.9171, lon: 10.7276, altitude: 30 } ``` The latitude and longitude cannot have more than 4 decimals, but that should be plenty. To quote yr: "There is no need to ask for weather forecasts with nanometer precision!". The altitude should be meters above sea level and defaults to 0. If `type` is set to `current` the symbol can display the next 1, 6 or 12 hours by setting `currentForecastHours` (default is 1). It states in [Getting started-guide](https://developer.yr.no/doc/GettingStarted/) that users of the API should cache the results and use the `Expires`-header to know when to ask for new data. By using the `If-Modified-Since`-header we can avoid downloading the same data over and over again. I chose not to override the `User-Agent`-header set in [`server.js`](https://github.com/MichMich/MagicMirror/blob/a328ce5/js/server.js#L97) even though it does not comply with [the terms of service](https://developer.yr.no/doc/TermsOfService/). It currently works with the default header, and by searching the web for MagicMirror the GitHub-repo should be easy to find without an explicit link. I also had to make some minor changes to `server.js` and `weatherprovider.js` to be able to send and return HTTP headers. To handle the HTTP 304 response without body I chose to return `undefined` so we easily can use the response as a condition: `if (response) ...`. The documentation for the API is available here: - [API Reference overview](https://api.met.no/weatherapi/) - [Locationforecast](https://api.met.no/weatherapi/locationforecast/2.0/) - Used to get the weather forecast - [Sunrise](https://api.met.no/weatherapi/sunrise/2.0/documentation) - used to find sunrise and sunset times Co-authored-by: Veeck <github@veeck.de>
2022-11-08 06:18:47 +01:00
const expectedHeaders = { headers: { header1: "value1", header2: "value2" } };
expect(fetchMock.mock.calls.length).toBe(1);
expect(fetchMock.mock.calls[0][1]).toStrictEqual(expectedHeaders);
});
});
describe("When receiving json format", () => {
test("Returns undefined when no data is received", async () => {
urlToCall = "www.test.com";
const response = await performWebRequest(urlToCall);
New weather provider: Yr.no (#2948) # Added Yr.no as a weather provider Yr.no is a free Norwegian weather service. The configuration is quite simple: ```js { weatherProvider: "yr", lat: 59.9171, lon: 10.7276, altitude: 30 } ``` The latitude and longitude cannot have more than 4 decimals, but that should be plenty. To quote yr: "There is no need to ask for weather forecasts with nanometer precision!". The altitude should be meters above sea level and defaults to 0. If `type` is set to `current` the symbol can display the next 1, 6 or 12 hours by setting `currentForecastHours` (default is 1). It states in [Getting started-guide](https://developer.yr.no/doc/GettingStarted/) that users of the API should cache the results and use the `Expires`-header to know when to ask for new data. By using the `If-Modified-Since`-header we can avoid downloading the same data over and over again. I chose not to override the `User-Agent`-header set in [`server.js`](https://github.com/MichMich/MagicMirror/blob/a328ce5/js/server.js#L97) even though it does not comply with [the terms of service](https://developer.yr.no/doc/TermsOfService/). It currently works with the default header, and by searching the web for MagicMirror the GitHub-repo should be easy to find without an explicit link. I also had to make some minor changes to `server.js` and `weatherprovider.js` to be able to send and return HTTP headers. To handle the HTTP 304 response without body I chose to return `undefined` so we easily can use the response as a condition: `if (response) ...`. The documentation for the API is available here: - [API Reference overview](https://api.met.no/weatherapi/) - [Locationforecast](https://api.met.no/weatherapi/locationforecast/2.0/) - Used to get the weather forecast - [Sunrise](https://api.met.no/weatherapi/sunrise/2.0/documentation) - used to find sunrise and sunset times Co-authored-by: Veeck <github@veeck.de>
2022-11-08 06:18:47 +01:00
expect(response).toBe(undefined);
});
test("Returns object when data is received", async () => {
urlToCall = "www.test.com";
New weather provider: Yr.no (#2948) # Added Yr.no as a weather provider Yr.no is a free Norwegian weather service. The configuration is quite simple: ```js { weatherProvider: "yr", lat: 59.9171, lon: 10.7276, altitude: 30 } ``` The latitude and longitude cannot have more than 4 decimals, but that should be plenty. To quote yr: "There is no need to ask for weather forecasts with nanometer precision!". The altitude should be meters above sea level and defaults to 0. If `type` is set to `current` the symbol can display the next 1, 6 or 12 hours by setting `currentForecastHours` (default is 1). It states in [Getting started-guide](https://developer.yr.no/doc/GettingStarted/) that users of the API should cache the results and use the `Expires`-header to know when to ask for new data. By using the `If-Modified-Since`-header we can avoid downloading the same data over and over again. I chose not to override the `User-Agent`-header set in [`server.js`](https://github.com/MichMich/MagicMirror/blob/a328ce5/js/server.js#L97) even though it does not comply with [the terms of service](https://developer.yr.no/doc/TermsOfService/). It currently works with the default header, and by searching the web for MagicMirror the GitHub-repo should be easy to find without an explicit link. I also had to make some minor changes to `server.js` and `weatherprovider.js` to be able to send and return HTTP headers. To handle the HTTP 304 response without body I chose to return `undefined` so we easily can use the response as a condition: `if (response) ...`. The documentation for the API is available here: - [API Reference overview](https://api.met.no/weatherapi/) - [Locationforecast](https://api.met.no/weatherapi/locationforecast/2.0/) - Used to get the weather forecast - [Sunrise](https://api.met.no/weatherapi/sunrise/2.0/documentation) - used to find sunrise and sunset times Co-authored-by: Veeck <github@veeck.de>
2022-11-08 06:18:47 +01:00
fetchResponse = new Response('{"body": "some content"}');
const response = await performWebRequest(urlToCall);
New weather provider: Yr.no (#2948) # Added Yr.no as a weather provider Yr.no is a free Norwegian weather service. The configuration is quite simple: ```js { weatherProvider: "yr", lat: 59.9171, lon: 10.7276, altitude: 30 } ``` The latitude and longitude cannot have more than 4 decimals, but that should be plenty. To quote yr: "There is no need to ask for weather forecasts with nanometer precision!". The altitude should be meters above sea level and defaults to 0. If `type` is set to `current` the symbol can display the next 1, 6 or 12 hours by setting `currentForecastHours` (default is 1). It states in [Getting started-guide](https://developer.yr.no/doc/GettingStarted/) that users of the API should cache the results and use the `Expires`-header to know when to ask for new data. By using the `If-Modified-Since`-header we can avoid downloading the same data over and over again. I chose not to override the `User-Agent`-header set in [`server.js`](https://github.com/MichMich/MagicMirror/blob/a328ce5/js/server.js#L97) even though it does not comply with [the terms of service](https://developer.yr.no/doc/TermsOfService/). It currently works with the default header, and by searching the web for MagicMirror the GitHub-repo should be easy to find without an explicit link. I also had to make some minor changes to `server.js` and `weatherprovider.js` to be able to send and return HTTP headers. To handle the HTTP 304 response without body I chose to return `undefined` so we easily can use the response as a condition: `if (response) ...`. The documentation for the API is available here: - [API Reference overview](https://api.met.no/weatherapi/) - [Locationforecast](https://api.met.no/weatherapi/locationforecast/2.0/) - Used to get the weather forecast - [Sunrise](https://api.met.no/weatherapi/sunrise/2.0/documentation) - used to find sunrise and sunset times Co-authored-by: Veeck <github@veeck.de>
2022-11-08 06:18:47 +01:00
expect(response.body).toBe("some content");
});
test("Returns expected headers when data is received", async () => {
urlToCall = "www.test.com";
New weather provider: Yr.no (#2948) # Added Yr.no as a weather provider Yr.no is a free Norwegian weather service. The configuration is quite simple: ```js { weatherProvider: "yr", lat: 59.9171, lon: 10.7276, altitude: 30 } ``` The latitude and longitude cannot have more than 4 decimals, but that should be plenty. To quote yr: "There is no need to ask for weather forecasts with nanometer precision!". The altitude should be meters above sea level and defaults to 0. If `type` is set to `current` the symbol can display the next 1, 6 or 12 hours by setting `currentForecastHours` (default is 1). It states in [Getting started-guide](https://developer.yr.no/doc/GettingStarted/) that users of the API should cache the results and use the `Expires`-header to know when to ask for new data. By using the `If-Modified-Since`-header we can avoid downloading the same data over and over again. I chose not to override the `User-Agent`-header set in [`server.js`](https://github.com/MichMich/MagicMirror/blob/a328ce5/js/server.js#L97) even though it does not comply with [the terms of service](https://developer.yr.no/doc/TermsOfService/). It currently works with the default header, and by searching the web for MagicMirror the GitHub-repo should be easy to find without an explicit link. I also had to make some minor changes to `server.js` and `weatherprovider.js` to be able to send and return HTTP headers. To handle the HTTP 304 response without body I chose to return `undefined` so we easily can use the response as a condition: `if (response) ...`. The documentation for the API is available here: - [API Reference overview](https://api.met.no/weatherapi/) - [Locationforecast](https://api.met.no/weatherapi/locationforecast/2.0/) - Used to get the weather forecast - [Sunrise](https://api.met.no/weatherapi/sunrise/2.0/documentation) - used to find sunrise and sunset times Co-authored-by: Veeck <github@veeck.de>
2022-11-08 06:18:47 +01:00
fetchResponse = new Response('{"body": "some content"}', { headers: { header1: "value1", header2: "value2" } });
const response = await performWebRequest(urlToCall, "json", false, undefined, ["header1"]);
New weather provider: Yr.no (#2948) # Added Yr.no as a weather provider Yr.no is a free Norwegian weather service. The configuration is quite simple: ```js { weatherProvider: "yr", lat: 59.9171, lon: 10.7276, altitude: 30 } ``` The latitude and longitude cannot have more than 4 decimals, but that should be plenty. To quote yr: "There is no need to ask for weather forecasts with nanometer precision!". The altitude should be meters above sea level and defaults to 0. If `type` is set to `current` the symbol can display the next 1, 6 or 12 hours by setting `currentForecastHours` (default is 1). It states in [Getting started-guide](https://developer.yr.no/doc/GettingStarted/) that users of the API should cache the results and use the `Expires`-header to know when to ask for new data. By using the `If-Modified-Since`-header we can avoid downloading the same data over and over again. I chose not to override the `User-Agent`-header set in [`server.js`](https://github.com/MichMich/MagicMirror/blob/a328ce5/js/server.js#L97) even though it does not comply with [the terms of service](https://developer.yr.no/doc/TermsOfService/). It currently works with the default header, and by searching the web for MagicMirror the GitHub-repo should be easy to find without an explicit link. I also had to make some minor changes to `server.js` and `weatherprovider.js` to be able to send and return HTTP headers. To handle the HTTP 304 response without body I chose to return `undefined` so we easily can use the response as a condition: `if (response) ...`. The documentation for the API is available here: - [API Reference overview](https://api.met.no/weatherapi/) - [Locationforecast](https://api.met.no/weatherapi/locationforecast/2.0/) - Used to get the weather forecast - [Sunrise](https://api.met.no/weatherapi/sunrise/2.0/documentation) - used to find sunrise and sunset times Co-authored-by: Veeck <github@veeck.de>
2022-11-08 06:18:47 +01:00
expect(response.headers.length).toBe(1);
expect(response.headers[0].name).toBe("header1");
expect(response.headers[0].value).toBe("value1");
});
});
} else {
test("Always ok, need one test", () => {});
}
});
describe("formatTime", () => {
const time = new Date();
beforeEach(async () => {
time.setHours(13, 13);
});
it("should convert correctly according to the config", () => {
expect(
formatTime(
{
timeFormat: 24
},
time
)
).toBe("13:13");
expect(
formatTime(
{
showPeriod: true,
showPeriodUpper: true,
timeFormat: 12
},
time
)
).toBe("1:13 PM");
expect(
formatTime(
{
showPeriod: true,
showPeriodUpper: false,
timeFormat: 12
},
time
)
).toBe("1:13 pm");
expect(
formatTime(
{
showPeriod: false,
timeFormat: 12
},
time
)
).toBe("1:13");
});
it("should convert correctly into another timezone", () => {
expect(
formatTime(
{
timeFormat: 24,
timezone: "America/Toronto"
},
time
)
).toBe("07:13");
});
});
New weather provider: Yr.no (#2948) # Added Yr.no as a weather provider Yr.no is a free Norwegian weather service. The configuration is quite simple: ```js { weatherProvider: "yr", lat: 59.9171, lon: 10.7276, altitude: 30 } ``` The latitude and longitude cannot have more than 4 decimals, but that should be plenty. To quote yr: "There is no need to ask for weather forecasts with nanometer precision!". The altitude should be meters above sea level and defaults to 0. If `type` is set to `current` the symbol can display the next 1, 6 or 12 hours by setting `currentForecastHours` (default is 1). It states in [Getting started-guide](https://developer.yr.no/doc/GettingStarted/) that users of the API should cache the results and use the `Expires`-header to know when to ask for new data. By using the `If-Modified-Since`-header we can avoid downloading the same data over and over again. I chose not to override the `User-Agent`-header set in [`server.js`](https://github.com/MichMich/MagicMirror/blob/a328ce5/js/server.js#L97) even though it does not comply with [the terms of service](https://developer.yr.no/doc/TermsOfService/). It currently works with the default header, and by searching the web for MagicMirror the GitHub-repo should be easy to find without an explicit link. I also had to make some minor changes to `server.js` and `weatherprovider.js` to be able to send and return HTTP headers. To handle the HTTP 304 response without body I chose to return `undefined` so we easily can use the response as a condition: `if (response) ...`. The documentation for the API is available here: - [API Reference overview](https://api.met.no/weatherapi/) - [Locationforecast](https://api.met.no/weatherapi/locationforecast/2.0/) - Used to get the weather forecast - [Sunrise](https://api.met.no/weatherapi/sunrise/2.0/documentation) - used to find sunrise and sunset times Co-authored-by: Veeck <github@veeck.de>
2022-11-08 06:18:47 +01:00
});