preface

The tokens and cookies we often use have an expiration date by default

When we do authentication, we rely on this a lot, so maybe we can be a little more rigorous

Because before are fixed format background, directly get the value to do a simple judgment;

That, if the background date format has changed!! Take a look if you’re interested.

Lead based

  • jest: This is a great testing framework, made by Facebook
  • ES5&&ES6
  • Typescript

We’re not going to talk about configuration, we’re not going to talk about other trivial things, just implementation

Thought analysis

The center of gravity is basically executing around passing parameters

  • To determine the type of parameter, consider only two cases
  • Number: Verifies whether it is a correct timestamp!!!!
  • String: Verifies whether there is onedatetimeFormat, or can be converted to a recognized format (e.g. 2018/08/01)
  • Type conversion and comparison
  • Finally, a Boolean value is returned to determine whether the value is valid

rendering

Code implementation

There isn’t much code to cover just a few cases, depending on the text description of the test

Js version

Isdate. js, which exposes the isDate function and takes one argument


function checkDateTime(d) {
  const _date = new Date(d);
  const Now = new Date().getTime();
  const DiffTime = _date.getTime() - Now;

  if (
    _date.getFullYear() === 1970 ||
    _date.getFullYear() < new Date().getFullYear()
  ) {
    // If the incoming date is converted to 1970... That's definitely not the time we're sending backstage
    // If the token is less than this year, it must not be.
    return false;
  }

  if (DiffTime > 60000) {
    The expiration time must be greater than the incoming time
    // When the expiration time is more than one minute,
    return true;
  } else {
    // Otherwise return false, call this function externally to get the return value,
    // Do a two-step process, renew or forcibly exit what the hell
    return false; }}/** * @description specifies the correct date * @param {*} d */
export const isDate = d= > {
  // Any argument that cannot be identified by Date returns NaN
  return isNaN(new Date(d).getTime()) || new Date(d).getTime() === 0
    ? false
    : checkDateTime(d);
};



Copy the code

Ts version

There will be an error in vscode

DateConstructor: Argument of type 'string | number' is not assignable to parameter of type 'string'.

Basically, dates can’t be assigned to strings. This problem seems to be waiting to be fixed. I looked it up on Github,

Github.com/Microsoft/T… .

Someone submitted PR, I don’t know if it was incorporated…

Github.com/Microsoft/T…


function checkDateTime(d: number | string) :boolean {
  const _date: Date = new Date(d);
  const Now: number = new Date().getTime();
  const DiffTime: number = _date.getTime() - Now;

  if (
    _date.getFullYear() === 1970 ||
    _date.getFullYear() < new Date().getFullYear()
  ) {
    // If the incoming date is converted to 1970... That's definitely not the time we're sending backstage
    // If the token is less than this year, it must not be.
    return false;
  }

  if (DiffTime > 60000) {
    // When the expiration time is more than one minute,
    return true;
  } else {
    // Otherwise return false, call this function externally to get the return value,
    // Do a two-step process, renew or forcibly exit what the hell
    return false; }}/** * @description specifies the correct date * @param {*} d */
export const isDate = (d: string | number) = > {
  // Any argument that cannot be identified by Date returns NaN
  return isNaN(new Date(d).getTime()) || new Date(d).getTime() === 0
    ? false
    : checkDateTime(d);
};


Copy the code

The test code

import { isDate } from ".. /.. /src/utils/isDate";

describe("IsDate function test set", () => {
  test("This non-standard timestamp will only revert to 1970, which is out of date.", () => {
    expect(isDate(21312445)).toBe(false);
  });
  test("Expired", () => {
    expect(isDate(1533097116565)).toBe(false);
  });
  test("Expired", () => {
    expect(isDate(1514764800000)).toBe(false);
  });
  test("Pass undefined as false, pass undefined as false.", () => {
    expect(isDate(undefined)).toBe(false);
  });
  test("Passing null returns 0, but it's false.", () => {
    expect(isDate(null)).toBe(false);
  });
  test("Standard format returns true", () => {
    expect(isDate("2018-12-01")).toBe(true);
  });
  test("Standard format returns true", () => {
    expect(isDate("2018/8/09")).toBe(true);
  });
  test("The old is wrong.", () => {
    expect(isDate("1988-10-21")).toBe(false);
  });
  test("Return false for nonstandard format", () => {
    expect(isDate("1970-13-51")).toBe(false);
  });
  test("Non-standard dates are also false", () => {
    expect(isDate("s2018ww-13-51")).toBe(false);
  });
  test("Normal strings will return fasle.", () => {
    expect(isDate("safdaserw")).toBe(false);
  });
});

Copy the code

conclusion

Pure function tests only declare inferred return values, so unit tests are very straightforward..

The nice thing about pure functions is that you can have low coupling, although we can have high cohesion here, doing things like renewals, requests, redirect, whatever,

So that’s all the functionality of an Auth, that’s not what I want,

If there is something wrong, please leave a message and correct it in time. Thank you for reading